Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): load missing fields from app deployment strategies #6140

Merged
merged 1 commit into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions engine/api/application/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (e dbApplication) Canonical() gorpmapper.CanonicalForms {
}

// LoadOptionFunc is a type for all options in LoadOptions
type LoadOptionFunc *func(gorp.SqlExecutor, *sdk.Application) error
type LoadOptionFunc *func(context.Context, gorp.SqlExecutor, *sdk.Application) error

// LoadOptions provides all options on project loads functions
var LoadOptions = struct {
Expand Down Expand Up @@ -137,14 +137,14 @@ func getWithClearVCSStrategyPassword(ctx context.Context, db gorp.SqlExecutor, k
return nil, err
}
if !isValid {
log.Error(context.Background(), "application.get> application %d data corrupted", dbApp.ID)
log.Error(ctx, "application.get> application %d data corrupted", dbApp.ID)
return nil, sdk.WithStack(sdk.ErrNotFound)
}
dbApp.ProjectKey = key
return unwrap(db, opts, &dbApp)
return unwrap(ctx, db, opts, &dbApp)
}

func unwrap(db gorp.SqlExecutor, opts []LoadOptionFunc, dbApp *dbApplication) (*sdk.Application, error) {
func unwrap(ctx context.Context, db gorp.SqlExecutor, opts []LoadOptionFunc, dbApp *dbApplication) (*sdk.Application, error) {
app := &dbApp.Application
if app.ProjectKey == "" {
pkey, errP := db.SelectStr("SELECT projectkey FROM project WHERE id = $1", app.ProjectID)
Expand All @@ -155,7 +155,7 @@ func unwrap(db gorp.SqlExecutor, opts []LoadOptionFunc, dbApp *dbApplication) (*
}

for _, f := range opts {
if err := (*f)(db, app); err != nil && sdk.Cause(err) != sql.ErrNoRows {
if err := (*f)(ctx, db, app); err != nil && sdk.Cause(err) != sql.ErrNoRows {
return nil, sdk.WrapError(err, "application.unwrap")
}
}
Expand Down Expand Up @@ -283,7 +283,7 @@ func getAllWithClearVCS(ctx context.Context, db gorp.SqlExecutor, opts []LoadOpt
continue
}
a := &res[i]
app, err := unwrap(db, opts, a)
app, err := unwrap(ctx, db, opts, a)
if err != nil {
return nil, sdk.WrapError(err, "application.getAllWithClearVCS")
}
Expand All @@ -310,7 +310,7 @@ func getAll(ctx context.Context, db gorp.SqlExecutor, opts []LoadOptionFunc, que
}

a := &res[i]
app, err := unwrap(db, opts, a)
app, err := unwrap(ctx, db, opts, a)
if err != nil {
return nil, sdk.WrapError(err, "application.getAll")
}
Expand Down
17 changes: 11 additions & 6 deletions engine/api/application/dao_application_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/rockbears/log"

"github.com/ovh/cds/engine/api/database/gorpmapping"
"github.com/ovh/cds/engine/api/integration"
"github.com/ovh/cds/engine/gorpmapper"
"github.com/ovh/cds/sdk"
)
Expand Down Expand Up @@ -46,15 +47,15 @@ func (e *dbApplicationDeploymentStrategy) IntegrationConfig() sdk.IntegrationCon
}

// LoadDeploymentStrategies loads the deployment strategies for an application
func LoadDeploymentStrategies(db gorp.SqlExecutor, appID int64, withClearPassword bool) (map[string]sdk.IntegrationConfig, error) {
func LoadDeploymentStrategies(ctx context.Context, db gorp.SqlExecutor, appID int64, withClearPassword bool) (map[string]sdk.IntegrationConfig, error) {
query := gorpmapping.NewQuery(`
SELECT *
FROM application_deployment_strategy
WHERE application_id = $1
`).Args(appID)

var res []dbApplicationDeploymentStrategy
if err := gorpmapping.GetAll(context.Background(), db, query, &res, gorpmapping.GetOptions.WithDecryption); err != nil {
if err := gorpmapping.GetAll(ctx, db, query, &res, gorpmapping.GetOptions.WithDecryption); err != nil {
return nil, sdk.WrapError(err, "unable to load deployment strategies")
}

Expand All @@ -65,7 +66,7 @@ func LoadDeploymentStrategies(db gorp.SqlExecutor, appID int64, withClearPasswor
return nil, err
}
if !isValid {
log.Error(context.Background(), "application.LoadDeploymentStrategies> application_deployment_strategy %d data corrupted", appID)
log.Error(ctx, "application.LoadDeploymentStrategies> application_deployment_strategy %d data corrupted", appID)
continue
}

Expand All @@ -85,12 +86,16 @@ func LoadDeploymentStrategies(db gorp.SqlExecutor, appID int64, withClearPasswor
newCfg[k] = v
}
}
// Sorry about that :(
projectIntegrationName, err := db.SelectStr("SELECT name FROM project_integration WHERE id = $1 ", r.ProjectIntegrationID)
projectIntegration, err := integration.LoadProjectIntegrationByID(ctx, db, r.ProjectIntegrationID)
if err != nil {
return nil, sdk.WrapError(err, "unable to find project integration name for ID=%d", r.ProjectIntegrationID)
}
deps[projectIntegrationName] = newCfg
for name, val := range projectIntegration.Model.AdditionalDefaultConfig {
if _, ok := newCfg[name]; !ok {
newCfg[name] = val
}
}
deps[projectIntegration.Name] = newCfg
}

return deps, nil
Expand Down
19 changes: 19 additions & 0 deletions engine/api/application/dao_application_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,23 @@ func Test_LoadAllDeploymentAllApps(t *testing.T) {
require.Len(t, deps[app2.ID], 1)
require.Equal(t, "secret1", deps[app1.ID][pp.ID]["token"].Value)
require.Equal(t, "secret2", deps[app2.ID][pp.ID]["token"].Value)

pf.AdditionalDefaultConfig = sdk.IntegrationConfig{
"token": sdk.IntegrationConfigValue{
Type: sdk.IntegrationConfigTypePassword,
Value: "my-secret-token",
},
"bar": sdk.IntegrationConfigValue{
Type: sdk.IntegrationConfigTypeString,
Value: "foo",
},
}

test.NoError(t, integration.UpdateModel(context.TODO(), db, &pf))

st, err := application.LoadDeploymentStrategies(context.TODO(), db, app1.ID, false)
require.NoError(t, err)
require.Len(t, st, 1)
require.Equal(t, st[pf.Name]["token"].Value, "**********")
require.Equal(t, st[pf.Name]["bar"].Value, "foo")
}
25 changes: 13 additions & 12 deletions engine/api/application/dao_dependencies.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package application

import (
"context"
"database/sql"

"github.com/go-gorp/gorp"
Expand All @@ -9,14 +10,14 @@ import (
)

var (
loadDefaultDependencies = func(db gorp.SqlExecutor, app *sdk.Application) error {
if err := loadVariables(db, app); err != nil && sdk.Cause(err) != sql.ErrNoRows {
loadDefaultDependencies = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
if err := loadVariables(ctx, db, app); err != nil && sdk.Cause(err) != sql.ErrNoRows {
return sdk.WrapError(err, "application.loadDefaultDependencies %s", app.Name)
}
return nil
}

loadVariables = func(db gorp.SqlExecutor, app *sdk.Application) error {
loadVariables = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
variables, err := LoadAllVariables(db, app.ID)
if err != nil && sdk.Cause(err) != sql.ErrNoRows {
return sdk.WrapError(err, "Unable to load variables for application %d", app.ID)
Expand All @@ -25,7 +26,7 @@ var (
return nil
}

loadVariablesWithClearPassword = func(db gorp.SqlExecutor, app *sdk.Application) error {
loadVariablesWithClearPassword = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
variables, err := LoadAllVariablesWithDecrytion(db, app.ID)
if err != nil && sdk.Cause(err) != sql.ErrNoRows {
return sdk.WrapError(err, "Unable to load variables for application %d", app.ID)
Expand All @@ -34,7 +35,7 @@ var (
return nil
}

loadKeys = func(db gorp.SqlExecutor, app *sdk.Application) error {
loadKeys = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
keys, err := LoadAllKeys(db, app.ID)
if err != nil {
return err
Expand All @@ -43,7 +44,7 @@ var (
return nil
}

loadClearKeys = func(db gorp.SqlExecutor, app *sdk.Application) error {
loadClearKeys = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
keys, err := LoadAllKeysWithPrivateContent(db, app.ID)
if err != nil {
return err
Expand All @@ -52,16 +53,16 @@ var (
return nil
}

loadDeploymentStrategies = func(db gorp.SqlExecutor, app *sdk.Application) error {
loadDeploymentStrategies = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
var err error
app.DeploymentStrategies, err = LoadDeploymentStrategies(db, app.ID, false)
app.DeploymentStrategies, err = LoadDeploymentStrategies(ctx, db, app.ID, false)
if err != nil && sdk.Cause(err) != sql.ErrNoRows {
return sdk.WrapError(err, "Unable to load deployment strategies for application %d", app.ID)
}
return nil
}

loadIcon = func(db gorp.SqlExecutor, app *sdk.Application) error {
loadIcon = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
var err error
app.Icon, err = LoadIcon(db, app.ID)
if err != nil && sdk.Cause(err) != sql.ErrNoRows {
Expand All @@ -70,7 +71,7 @@ var (
return nil
}

loadVulnerabilities = func(db gorp.SqlExecutor, app *sdk.Application) error {
loadVulnerabilities = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
var err error
app.Vulnerabilities, err = LoadVulnerabilities(db, app.ID)
if err != nil {
Expand All @@ -79,9 +80,9 @@ var (
return nil
}

loadDeploymentStrategiesWithClearPassword = func(db gorp.SqlExecutor, app *sdk.Application) error {
loadDeploymentStrategiesWithClearPassword = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
var err error
app.DeploymentStrategies, err = LoadDeploymentStrategies(db, app.ID, true)
app.DeploymentStrategies, err = LoadDeploymentStrategies(ctx, db, app.ID, true)
if err != nil && sdk.Cause(err) != sql.ErrNoRows {
return sdk.WrapError(err, "Unable to load deployment strategies for application %d", app.ID)
}
Expand Down
2 changes: 1 addition & 1 deletion engine/api/application_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func Test_postApplicationDeploymentStrategyConfigHandlerAsProvider(t *testing.T)
})
test.NoError(t, err)

cfg, err := application.LoadDeploymentStrategies(api.mustDB(), app.ID, true)
cfg, err := application.LoadDeploymentStrategies(context.TODO(), api.mustDB(), app.ID, true)
test.NoError(t, err)

var assertCfg = func(key string, cfg sdk.IntegrationConfig, expected sdk.IntegrationConfigValue) {
Expand Down
6 changes: 3 additions & 3 deletions engine/api/project/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func loadApplicationWithDeploymentStrategies(ctx context.Context, db gorp.SqlExe
}
for i := range proj.Applications {
a := &proj.Applications[i]
if err := (*application.LoadOptions.WithDeploymentStrategies)(db, a); err != nil {
if err := (*application.LoadOptions.WithDeploymentStrategies)(ctx, db, a); err != nil {
return sdk.WithStack(err)
}
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func loadApplicationVariables(ctx context.Context, db gorp.SqlExecutor, proj *sd
}

for _, a := range proj.Applications {
if err := (*application.LoadOptions.WithVariables)(db, &a); err != nil {
if err := (*application.LoadOptions.WithVariables)(ctx, db, &a); err != nil {
return sdk.WithStack(err)
}
}
Expand All @@ -150,7 +150,7 @@ func loadApplicationKeys(ctx context.Context, db gorp.SqlExecutor, proj *sdk.Pro
}

for _, a := range proj.Applications {
if err := (*application.LoadOptions.WithKeys)(db, &a); err != nil {
if err := (*application.LoadOptions.WithKeys)(ctx, db, &a); err != nil {
return sdk.WithStack(err)
}
}
Expand Down