Skip to content

Commit ed16cfc

Browse files
committed
fix(mnq): fix XDG_CONFIG_HOME compliance in NATS context directory
1 parent 5faf21b commit ed16cfc

File tree

3 files changed

+268
-2
lines changed

3 files changed

+268
-2
lines changed

internal/namespaces/mnq/v1beta1/custom_nats_test.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/scaleway/scaleway-cli/v2/core"
1111
mnq "github.com/scaleway/scaleway-cli/v2/internal/namespaces/mnq/v1beta1"
12+
"github.com/scaleway/scaleway-sdk-go/scw"
1213
"github.com/stretchr/testify/assert"
1314
)
1415

@@ -108,12 +109,66 @@ func Test_CreateContextNoInteractiveTermAndMultiAccount(t *testing.T) {
108109
}))
109110
}
110111

112+
func beforeFuncCopyConfigToTmpHome() core.BeforeFunc {
113+
return core.BeforeFuncWhenUpdatingCassette(func(ctx *core.BeforeFuncCtx) error {
114+
realHomeDir, err := os.UserHomeDir()
115+
if err != nil {
116+
return err
117+
}
118+
119+
realConfigPath := filepath.Join(realHomeDir, ".config", "scw", "config.yaml")
120+
if _, err := os.Stat(realConfigPath); os.IsNotExist(err) {
121+
return nil
122+
}
123+
124+
config, err := scw.LoadConfigFromPath(realConfigPath)
125+
if err != nil {
126+
return err
127+
}
128+
129+
activeProfile, err := config.GetActiveProfile()
130+
if err != nil {
131+
return err
132+
}
133+
134+
tmpHomeDir := ctx.OverrideEnv["HOME"]
135+
tmpConfigDir := filepath.Join(tmpHomeDir, ".config", "scw")
136+
if err := os.MkdirAll(tmpConfigDir, 0o0755); err != nil {
137+
return err
138+
}
139+
140+
tmpConfigPath := filepath.Join(tmpConfigDir, "config.yaml")
141+
142+
if err := config.SaveTo(tmpConfigPath); err != nil {
143+
return err
144+
}
145+
146+
if activeProfile.AccessKey != nil {
147+
ctx.OverrideEnv[scw.ScwAccessKeyEnv] = *activeProfile.AccessKey
148+
}
149+
if activeProfile.SecretKey != nil {
150+
ctx.OverrideEnv[scw.ScwSecretKeyEnv] = *activeProfile.SecretKey
151+
}
152+
if activeProfile.DefaultOrganizationID != nil {
153+
ctx.OverrideEnv[scw.ScwDefaultOrganizationIDEnv] = *activeProfile.DefaultOrganizationID
154+
}
155+
if activeProfile.DefaultProjectID != nil {
156+
ctx.OverrideEnv[scw.ScwDefaultProjectIDEnv] = *activeProfile.DefaultProjectID
157+
}
158+
159+
return nil
160+
})
161+
}
162+
111163
func Test_CreateContextWithXDGConfigHome(t *testing.T) {
112164
xdgConfigHomeDir := t.TempDir()
113165

114166
t.Run("XDG_CONFIG_HOME compliance", core.Test(&core.TestConfig{
115-
Commands: mnq.GetCommands(),
116-
BeforeFunc: createNATSAccount("NATS"),
167+
Commands: mnq.GetCommands(),
168+
BeforeFunc: core.BeforeFuncCombine(
169+
beforeFuncCopyConfigToTmpHome(),
170+
createNATSAccount("NATS"),
171+
),
117172
Cmd: "scw mnq nats create-context nats-account-id={{ .NATS.ID }}",
118173
TmpHomeDir: true,
119174
OverrideEnv: map[string]string{
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: '{"project_id":"46fd79d8-1a35-4548-bfb8-03df51a0ebae", "region":"fr-par",
6+
"created_at":"2025-12-04T05:08:07.797431533Z", "updated_at":"2025-12-04T05:08:07.797431533Z",
7+
"id":"AAFJH7WGGU2YRNIXDC5IRRBOFYMPU6D5UJLGPWCVI3VWIJQFLOX2JB75", "name":"cli-mnq-vibrant-antonelli",
8+
"endpoint":"nats://nats.mnq.fr-par.scaleway.com:4222"}'
9+
form: {}
10+
headers:
11+
Content-Type:
12+
- application/json
13+
User-Agent:
14+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.25.3; darwin; amd64) cli-e2e-test
15+
url: https://api.scaleway.com/mnq/v1beta1/regions/fr-par/nats-accounts
16+
method: POST
17+
response:
18+
body: '{"project_id":"46fd79d8-1a35-4548-bfb8-03df51a0ebae", "region":"fr-par",
19+
"created_at":"2025-12-04T05:08:07.797431533Z", "updated_at":"2025-12-04T05:08:07.797431533Z",
20+
"id":"AAFJH7WGGU2YRNIXDC5IRRBOFYMPU6D5UJLGPWCVI3VWIJQFLOX2JB75", "name":"cli-mnq-vibrant-antonelli",
21+
"endpoint":"nats://nats.mnq.fr-par.scaleway.com:4222"}'
22+
headers:
23+
Content-Length:
24+
- "322"
25+
Content-Security-Policy:
26+
- default-src 'none'; frame-ancestors 'none'
27+
Content-Type:
28+
- application/json
29+
Date:
30+
- Thu, 04 Dec 2025 05:08:07 GMT
31+
Server:
32+
- Scaleway API Gateway (fr-par-2;edge03)
33+
Strict-Transport-Security:
34+
- max-age=63072000
35+
X-Content-Type-Options:
36+
- nosniff
37+
X-Frame-Options:
38+
- DENY
39+
X-Request-Id:
40+
- edffd555-1943-4ed1-a5bf-76b020ab1d9b
41+
status: 200 OK
42+
code: 200
43+
duration: ""
44+
- request:
45+
body: '{"access_key":"SCW8XSQX472BP8SNX6HA", "secret_key":null, "description":"",
46+
"created_at":"2025-11-21T09:36:03.459154Z", "updated_at":"2025-11-21T09:36:03.459154Z",
47+
"expires_at":null, "default_project_id":"46fd79d8-1a35-4548-bfb8-03df51a0ebae",
48+
"editable":true, "deletable":true, "managed":false, "creation_ip":"51.159.46.153",
49+
"user_id":"a52be78d-a4f2-4d4f-a4e6-8bf74589bfd8"}'
50+
form: {}
51+
headers:
52+
User-Agent:
53+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.25.3; darwin; amd64) cli-e2e-test
54+
url: https://api.scaleway.com/iam/v1alpha1/api-keys/SCW8XSQX472BP8SNX6HA
55+
method: GET
56+
response:
57+
body: '{"access_key":"SCW8XSQX472BP8SNX6HA", "secret_key":null, "description":"",
58+
"created_at":"2025-11-21T09:36:03.459154Z", "updated_at":"2025-11-21T09:36:03.459154Z",
59+
"expires_at":null, "default_project_id":"46fd79d8-1a35-4548-bfb8-03df51a0ebae",
60+
"editable":true, "deletable":true, "managed":false, "creation_ip":"51.159.46.153",
61+
"user_id":"a52be78d-a4f2-4d4f-a4e6-8bf74589bfd8"}'
62+
headers:
63+
Content-Length:
64+
- "375"
65+
Content-Security-Policy:
66+
- default-src 'none'; frame-ancestors 'none'
67+
Content-Type:
68+
- application/json
69+
Date:
70+
- Thu, 04 Dec 2025 05:08:08 GMT
71+
Server:
72+
- Scaleway API Gateway (fr-par-2;edge03)
73+
Strict-Transport-Security:
74+
- max-age=63072000
75+
X-Content-Type-Options:
76+
- nosniff
77+
X-Frame-Options:
78+
- DENY
79+
X-Request-Id:
80+
- 8031e99a-c130-455c-a74b-a48a68c9c92a
81+
status: 200 OK
82+
code: 200
83+
duration: ""
84+
- request:
85+
body: '{"project_id":"46fd79d8-1a35-4548-bfb8-03df51a0ebae", "region":"fr-par",
86+
"created_at":"2025-12-04T05:08:07.797431Z", "updated_at":"2025-12-04T05:08:07.797431Z",
87+
"id":"AAFJH7WGGU2YRNIXDC5IRRBOFYMPU6D5UJLGPWCVI3VWIJQFLOX2JB75", "name":"cli-mnq-vibrant-antonelli",
88+
"endpoint":"nats://nats.mnq.fr-par.scaleway.com:4222"}'
89+
form: {}
90+
headers:
91+
User-Agent:
92+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.25.3; darwin; amd64) cli-e2e-test
93+
url: https://api.scaleway.com/mnq/v1beta1/regions/fr-par/nats-accounts/AAFJH7WGGU2YRNIXDC5IRRBOFYMPU6D5UJLGPWCVI3VWIJQFLOX2JB75
94+
method: GET
95+
response:
96+
body: '{"project_id":"46fd79d8-1a35-4548-bfb8-03df51a0ebae", "region":"fr-par",
97+
"created_at":"2025-12-04T05:08:07.797431Z", "updated_at":"2025-12-04T05:08:07.797431Z",
98+
"id":"AAFJH7WGGU2YRNIXDC5IRRBOFYMPU6D5UJLGPWCVI3VWIJQFLOX2JB75", "name":"cli-mnq-vibrant-antonelli",
99+
"endpoint":"nats://nats.mnq.fr-par.scaleway.com:4222"}'
100+
headers:
101+
Content-Length:
102+
- "316"
103+
Content-Security-Policy:
104+
- default-src 'none'; frame-ancestors 'none'
105+
Content-Type:
106+
- application/json
107+
Date:
108+
- Thu, 04 Dec 2025 05:08:08 GMT
109+
Server:
110+
- Scaleway API Gateway (fr-par-2;edge03)
111+
Strict-Transport-Security:
112+
- max-age=63072000
113+
X-Content-Type-Options:
114+
- nosniff
115+
X-Frame-Options:
116+
- DENY
117+
X-Request-Id:
118+
- 6eeac215-6850-424b-b7c3-8ef6fda6184e
119+
status: 200 OK
120+
code: 200
121+
duration: ""
122+
- request:
123+
body: '{"nats_account_id":"AAFJH7WGGU2YRNIXDC5IRRBOFYMPU6D5UJLGPWCVI3VWIJQFLOX2JB75",
124+
"region":"fr-par", "created_at":"2025-12-04T05:08:08.325079040Z", "updated_at":"2025-12-04T05:08:08.325079290Z",
125+
"id":"d21c9f77-e065-490e-b4fc-a2feb3e4d90f", "name":"cli-mnq-vibrant-antonellicli-creds-priceless-bhabha",
126+
"credentials":{"name":"user.creds", "content":"-----BEGIN NATS USER JWT-----\neyJ0eXAiOiJKV1QiLCJhbGciOiJlZDI1NTE5LW5rZXkifQ.eyJqdGkiOiJSNDNZRVhBT1NZTExJUkFDR01WUDJMU1JDWEMyWVk2UlVQN1hQRUxDNlBJQ1pQNlRLNEtBIiwiaWF0IjoxNzY0ODI0ODg4LCJpc3MiOiJBREFXV0dMVVJLTVdYWlYyVldJWktDNkMzUUNZMjNKN1BJRU5BTDZaUlhSRzJLTUtKT0FUSEJNSSIsIm5hbWUiOiJjbGktbW5xLXZpYnJhbnQtYW50b25lbGxpY2xpLWNyZWRzLXByaWNlbGVzcy1iaGFiaGEiLCJzdWIiOiJVQkFYVzMzQTc2UFY3SUQ0UUtHWFdaUkpSQlJRT1ZHWVNCM1NRNDVQQldNUFBBT0VOMlFMSlJZVyIsIm5hdHMiOnsicHViIjp7fSwic3ViIjp7fSwic3VicyI6LTEsImRhdGEiOi0xLCJwYXlsb2FkIjotMSwiaXNzdWVyX2FjY291bnQiOiJBQUZKSDdXR0dVMllSTklYREM1SVJSQk9GWU1QVTZENVVKTEdQV0NWSTNWV0lKUUZMT1gySkI3NSIsInR5cGUiOiJ1c2VyIiwidmVyc2lvbiI6Mn19.zaqi9Sf5iBSWXbj-THzcti2GFxpim8O_MhgWsYbDIEc0M39Er-Wgyspw4APxYDsqqBQRNnKfaYSxmUbz-mPUBg\n------END
127+
NATS USER JWT------\n\n************************* IMPORTANT *************************\nNKEY
128+
Seed printed below can be used to sign and prove identity.\nNKEYs are sensitive
129+
and should be treated as secrets.\n\n-----BEGIN USER NKEY SEED-----\nSUAGEIKEUA7L46FYGMVMDQSQEBIVRPJAHEEC23SEMCAR4X5VFAXQPPPC7Q\n------END
130+
USER NKEY SEED------\n\n*************************************************************\n"},
131+
"checksum":"1313cedb5035d6df3ad1b43112fd6b0ba32dd1dd"}'
132+
form: {}
133+
headers:
134+
Content-Type:
135+
- application/json
136+
User-Agent:
137+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.25.3; darwin; amd64) cli-e2e-test
138+
url: https://api.scaleway.com/mnq/v1beta1/regions/fr-par/nats-credentials
139+
method: POST
140+
response:
141+
body: '{"nats_account_id":"AAFJH7WGGU2YRNIXDC5IRRBOFYMPU6D5UJLGPWCVI3VWIJQFLOX2JB75",
142+
"region":"fr-par", "created_at":"2025-12-04T05:08:08.325079040Z", "updated_at":"2025-12-04T05:08:08.325079290Z",
143+
"id":"d21c9f77-e065-490e-b4fc-a2feb3e4d90f", "name":"cli-mnq-vibrant-antonellicli-creds-priceless-bhabha",
144+
"credentials":{"name":"user.creds", "content":"-----BEGIN NATS USER JWT-----\neyJ0eXAiOiJKV1QiLCJhbGciOiJlZDI1NTE5LW5rZXkifQ.eyJqdGkiOiJSNDNZRVhBT1NZTExJUkFDR01WUDJMU1JDWEMyWVk2UlVQN1hQRUxDNlBJQ1pQNlRLNEtBIiwiaWF0IjoxNzY0ODI0ODg4LCJpc3MiOiJBREFXV0dMVVJLTVdYWlYyVldJWktDNkMzUUNZMjNKN1BJRU5BTDZaUlhSRzJLTUtKT0FUSEJNSSIsIm5hbWUiOiJjbGktbW5xLXZpYnJhbnQtYW50b25lbGxpY2xpLWNyZWRzLXByaWNlbGVzcy1iaGFiaGEiLCJzdWIiOiJVQkFYVzMzQTc2UFY3SUQ0UUtHWFdaUkpSQlJRT1ZHWVNCM1NRNDVQQldNUFBBT0VOMlFMSlJZVyIsIm5hdHMiOnsicHViIjp7fSwic3ViIjp7fSwic3VicyI6LTEsImRhdGEiOi0xLCJwYXlsb2FkIjotMSwiaXNzdWVyX2FjY291bnQiOiJBQUZKSDdXR0dVMllSTklYREM1SVJSQk9GWU1QVTZENVVKTEdQV0NWSTNWV0lKUUZMT1gySkI3NSIsInR5cGUiOiJ1c2VyIiwidmVyc2lvbiI6Mn19.zaqi9Sf5iBSWXbj-THzcti2GFxpim8O_MhgWsYbDIEc0M39Er-Wgyspw4APxYDsqqBQRNnKfaYSxmUbz-mPUBg\n------END
145+
NATS USER JWT------\n\n************************* IMPORTANT *************************\nNKEY
146+
Seed printed below can be used to sign and prove identity.\nNKEYs are sensitive
147+
and should be treated as secrets.\n\n-----BEGIN USER NKEY SEED-----\nSUAGEIKEUA7L46FYGMVMDQSQEBIVRPJAHEEC23SEMCAR4X5VFAXQPPPC7Q\n------END
148+
USER NKEY SEED------\n\n*************************************************************\n"},
149+
"checksum":"1313cedb5035d6df3ad1b43112fd6b0ba32dd1dd"}'
150+
headers:
151+
Content-Length:
152+
- "1554"
153+
Content-Security-Policy:
154+
- default-src 'none'; frame-ancestors 'none'
155+
Content-Type:
156+
- application/json
157+
Date:
158+
- Thu, 04 Dec 2025 05:08:08 GMT
159+
Server:
160+
- Scaleway API Gateway (fr-par-2;edge03)
161+
Strict-Transport-Security:
162+
- max-age=63072000
163+
X-Content-Type-Options:
164+
- nosniff
165+
X-Frame-Options:
166+
- DENY
167+
X-Request-Id:
168+
- 628cf359-ba2b-4963-9d5d-4b20dec718e4
169+
status: 200 OK
170+
code: 200
171+
duration: ""
172+
- request:
173+
body: ""
174+
form: {}
175+
headers:
176+
User-Agent:
177+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.25.3; darwin; amd64) cli-e2e-test
178+
url: https://api.scaleway.com/mnq/v1beta1/regions/fr-par/nats-accounts/AAFJH7WGGU2YRNIXDC5IRRBOFYMPU6D5UJLGPWCVI3VWIJQFLOX2JB75
179+
method: DELETE
180+
response:
181+
body: ""
182+
headers:
183+
Content-Security-Policy:
184+
- default-src 'none'; frame-ancestors 'none'
185+
Content-Type:
186+
- application/json
187+
Date:
188+
- Thu, 04 Dec 2025 05:08:08 GMT
189+
Server:
190+
- Scaleway API Gateway (fr-par-2;edge03)
191+
Strict-Transport-Security:
192+
- max-age=63072000
193+
X-Content-Type-Options:
194+
- nosniff
195+
X-Frame-Options:
196+
- DENY
197+
X-Request-Id:
198+
- 19e34f27-9202-436b-9f8b-884e58cb49a4
199+
status: 204 No Content
200+
code: 204
201+
duration: ""
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
✅ Nats context successfully created.
4+
credential-placeholder nats credentials was created
5+
Select context using `nats context select context-placeholder`
6+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
7+
{
8+
"message": "Nats context successfully created",
9+
"details": "credential-placeholder nats credentials was created\nSelect context using `nats context select context-placeholder`"
10+
}

0 commit comments

Comments
 (0)