8000 fix(api): save previous key clear value when using regen to false (#3… · ovh/cds@e02c584 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit e02c584

Browse files
richardltsguiheux
authored andcommitted
fix(api): save previous key clear value when using regen to false (#3918)
1 parent 69c3bdf commit e02c584

File tree

2 files changed

+67
-29
lines changed

2 files changed

+67
-29
lines changed

engine/api/application/application_parser.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ func ParseAndImport(db gorp.SqlExecutor, cache cache.Store, proj *sdk.Project, e
2424
}
2525

2626
//Check if app exist
27-
oldApp, errl := LoadByName(db, cache, proj.Key, eapp.Name, nil, LoadOptions.WithVariablesWithClearPassword, LoadOptions.WithKeys, LoadOptions.WithClearDeploymentStrategies)
27+
oldApp, errl := LoadByName(db, cache, proj.Key, eapp.Name, nil,
28+
LoadOptions.WithVariablesWithClearPassword,
29+
LoadOptions.WithClearKeys,
30+
LoadOptions.WithClearDeploymentStrategies,
31+
)
2832
if errl != nil && !sdk.ErrorIs(errl, sdk.ErrApplicationNotFound) {
2933
return nil, nil, sdk.WrapError(errl, "unable to load application")
3034
}

engine/api/application_import_test.go

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecretsAndReImpo
379379
}
380380

381381
func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecretsAndReImportWithRegen(t *testing.T) {
382+
// init project and application for test
382383
api, db, _, end := newTestAPI(t)
383384
defer end()
384385

@@ -391,35 +392,61 @@ func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecretsAndReImpo
391392
}
392393
test.NoError(t, application.Insert(db, api.Cache, proj, app, u))
393394

394-
k := &sdk.ApplicationKey{
395+
// create password, pgp and ssh keys
396+
k1 := &sdk.ApplicationKey{
395397
Key: sdk.Key{
396-
Name: "app-mykey",
398+
Name: "app-key-1",
397399
Type: "pgp",
398400
},
399401
ApplicationID: app.ID,
400402
}
401403

402-
kpgp, err := keys.GeneratePGPKeyPair(k.Name)
404+
kpgp, err := keys.GeneratePGPKeyPair(k1.Name)
403405
test.NoError(t, err)
404-
k.Public = kpgp.Public
405-
k.Private = kpgp.Private
406-
k.KeyID = kpgp.KeyID
407-
if err := application.InsertKey(api.mustDB(), k); err != nil {
408-
t.Fatal(err)
406+
k1.Public = kpgp.Public
407+
k1.Private = kpgp.Private
408+
k1.KeyID = kpgp.KeyID
409+
test.NoError(t, application.InsertKey(api.mustDB(), k1))
410+
411+
// create password, pgp and ssh keys
412+
k2 := &sdk.ApplicationKey{
413+
Key: sdk.Key{
414+
Name: "app-key-2",
415+
Type: "ssh",
416+
},
417+
ApplicationID: app.ID,
409418
}
410419

420+
kssh, err := keys.GenerateSSHKey(k2.Name)
421+
test.NoError(t, err)
422+
k2.Public = kssh.Public
423+
k2.Private = kssh.Private
424+
k2.KeyID = kssh.KeyID
425+
test.NoError(t, application.InsertKey(api.mustDB(), k2))
426+
411427
test.NoError(t, application.InsertVariable(api.mustDB(), api.Cache, app, sdk.Variable{
412428
Name: "myPassword",
413429
Type: sdk.SecretVariable,
414430
Value: "MySecretValue",
415431
}, u))
416432

417-
//Export all the things
418-
vars := map[string]string{
433+
// check that keys secrets are well stored
434+
app, err = application.LoadByName(db, api.Cache, proj.Key, "myNewApp", nil,
435+
application.LoadOptions.WithClearKeys,
436+
application.LoadOptions.WithVariablesWithClearPassword,
437+
)
438+
test.NoError(t, err)
439+
test.Equal(t, 1, len(app.Variable))
440+
test.Equal(t, "MySecretValue", app.Variable[0].Value)
441+
test.Equal(t, 2, len(app.Keys))
442+
test.Equal(t, kpgp.Private, app.Keys[0].Private)
443+
test.Equal(t, kssh.Private, app.Keys[1].Private)
444+
445+
// export the app then import it with regen false
446+
uri := api.Router.GetRoute("GET", api.getApplicationExportHandler, map[string]string{
419447
"key": proj.Key,
420448
"permApplicationName": app.Name,
421-
}
422-
uri := api.Router.GetRoute("GET", api.getApplicationExportHandler, vars)
449+
})
423450
test.NotEmpty(t, uri)
424451
req := assets.NewAuthentifiedRequest(t, u, pass, "GET", uri, nil)
425452

@@ -434,44 +461,51 @@ func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecretsAndReImpo
434461

435462
eapp := &exportentities.Application{}
436463
test.NoError(t, yaml.Unmarshal([]byte(body), eapp))
464+
test.Equal(t, 1, len(eapp.Variables))
465+
test.Equal(t, 2, len(eapp.Keys))
437466

438467
False := false
439-
ek := eapp.Keys[k.Name]
440-
ek.Regen = &False
441-
ek.Value = ""
442-
eapp.Keys[k.Name] = ek
468+
ek1 := eapp.Keys[k1.Name]
469+
ek1.Regen = &False
470+
ek1.Value = ""
471+
eapp.Keys[k1.Name] = ek1
472+
473+
ek2 := eapp.Keys[k2.Name]
474+
ek2.Regen = &False
475+
ek2.Value = ""
476+
eapp.Keys[k2.Name] = ek2
443477

444478
btes, err := yaml.Marshal(eapp)
445479
body = string(btes)
446480

447481
t.Log(body)
448482

449-
//Import the new application
450-
vars = map[string]string{
483+
// import the new application then check secrets values.
484+
uri = api.Router.GetRoute("POST", api.postApplicationImportHandler, map[string]string{
451485
"permProjectKey": proj.Key,
452-
}
453-
uri = api.Router.GetRoute("POST", api.postApplicationImportHandler, vars)
486+
})
454487
test.NotEmpty(t, uri)
455488
uri += "?force=true"
456489
req = assets.NewAuthentifiedRequest(t, u, pass, "POST", uri, nil)
457490
req.Body = ioutil.NopCloser(strings.NewReader(body))
458491
req.Header.Set("Content-Type", "application/x-yaml")
459492

460-
//Do the request
461493
rec = httptest.NewRecorder()
462494
api.Router.Mux.ServeHTTP(rec, req)
463495
assert.Equal(t, 200, rec.Code)
464496

465-
//Check result
466497
t.Logf(">>%s", rec.Body.String())
467498

468-
app, err = application.LoadByName(db, api.Cache, proj.Key, "myNewApp", nil, application.LoadOptions.WithKeys, application.LoadOptions.WithVariablesWithClearPassword)
499+
app, err = application.LoadByName(db, api.Cache, proj.Key, "myNewApp", nil,
500+
application.LoadOptions.WithClearKeys,
501+
application.LoadOptions.WithVariablesWithClearPassword,
502+
)
469503
test.NoError(t, err)
470-
//Check keys
471-
for _, k := range app.Keys {
472-
assert.NotEmpty(t, k.Private)
473-
assert.NotEmpty(t, k.Public)
474-
}
504+
test.Equal(t, 1, len(app.Variable))
505+
test.Equal(t, "MySecretValue", app.Variable[0].Value)
506+
test.Equal(t, 2, len(app.Keys))
507+
test.Equal(t, kpgp.Private, app.Keys[0].Private)
508+
test.Equal(t, kssh.Private, app.Keys[1].Private)
475509
}
476510

477511
func Test_postApplicationImportHandler_NewAppFromYAMLWithEmptyKey(t *testing.T) {

0 commit comments

Comments
 (0)
0