Skip to content

Commit

Permalink
fix(api): import application with deployment strategies secrets (#3146)
Browse files Browse the repository at this point in the history
closes #3129
  • Loading branch information
fsamin authored and yesnault committed Aug 6, 2018
1 parent 786101b commit 066be05
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
43 changes: 32 additions & 11 deletions engine/api/application/application_parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package application

import (
"fmt"
"strings"
"sync"

Expand Down Expand Up @@ -142,19 +143,39 @@ func ParseAndImport(db gorp.SqlExecutor, cache cache.Store, proj *sdk.Project, e

//deployment strategies
for pfName, pfConfig := range eapp.DeploymentStrategies {
if app.DeploymentStrategies == nil {
app.DeploymentStrategies = make(map[string]sdk.PlatformConfig)
}
if app.DeploymentStrategies[pfName] == nil {
app.DeploymentStrategies[pfName] = make(map[string]sdk.PlatformConfigValue)
}

projPF, has := proj.GetPlatform(pfName)
if !has {
return app, nil, sdk.WrapError(sdk.NewError(sdk.ErrWrongRequest, fmt.Errorf("platform not found")), "ParseAndImport> Platform %s not found", pfName)
}

// Inherit from existing deployment strategy or from the project
if oldApp != nil {
oldPFConfig, has := oldApp.DeploymentStrategies[pfName]
if has {
app.DeploymentStrategies[pfName] = oldPFConfig.Clone()
}
} else {
app.DeploymentStrategies[pfName] = projPF.Model.DeploymentDefaultConfig.Clone()
}

app.DeploymentStrategies[pfName].MergeWith(projPF.Model.DeploymentDefaultConfig.Clone())

for k, v := range pfConfig {
if v.Type == sdk.SecretVariable {
clearPWD, err := decryptFunc(db, proj.ID, v.Value)
if err != nil {
return app, nil, sdk.WrapError(sdk.NewError(sdk.ErrWrongRequest, err), "ParseAndImport> Unable to decrypt deployment strategy password")
if v.Value != "" {
if v.Type == sdk.SecretVariable {
clearPWD, err := decryptFunc(db, proj.ID, v.Value)
if err != nil {
return app, nil, sdk.WrapError(sdk.NewError(sdk.ErrWrongRequest, err), "ParseAndImport> Unable to decrypt deployment strategy password")
}
v.Value = clearPWD
}
v.Value = clearPWD
}
if app.DeploymentStrategies == nil {
app.DeploymentStrategies = make(map[string]sdk.PlatformConfig)
}
if app.DeploymentStrategies[pfName] == nil {
app.DeploymentStrategies[pfName] = make(map[string]sdk.PlatformConfigValue)
}
app.DeploymentStrategies[pfName][k] = sdk.PlatformConfigValue{
Type: v.Type,
Expand Down
10 changes: 6 additions & 4 deletions engine/api/project/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,19 @@ func loadprojects(db gorp.SqlExecutor, store cache.Store, u *sdk.User, opts []Lo
return nil, sdk.WrapError(err, "loadprojects> db.Select")
}

projs := make([]sdk.Project, len(res))
projs := make([]sdk.Project, 0, len(res))
for i := range res {
p := &res[i]
if err := p.PostGet(db); err != nil {
return nil, sdk.WrapError(err, "loadprojects> PostGet error")
log.Error("loadprojects> PostGet error (ID=%d, Key:%s): %v", p.ID, p.Key, err)
continue
}
proj, err := unwrap(db, store, p, u, opts)
if err != nil {
return nil, sdk.WrapError(err, "loadprojects> unwrap error")
log.Error("loadprojects> unwrap error (ID=%d, Key:%s): %v", p.ID, p.Key, err)
continue
}
projs[i] = *proj
projs = append(projs, *proj)
}

return projs, nil
Expand Down
2 changes: 2 additions & 0 deletions sdk/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ func (config PlatformConfig) MergeWith(cfg PlatformConfig) {
val, has := config[k]
if !has {
val.Type = v.Type
} else if v.Value == "" && config[k].Value != "" {
continue
}
if val.Type != PlatformConfigTypePassword || (val.Type == PlatformConfigTypePassword && v.Value != PasswordPlaceholder) {
val.Value = v.Value
Expand Down

0 comments on commit 066be05

Please sign in to comment.