Merge pull request #119 from sgotti/gateway_api_getsecrets_implement_removeoverridden

gateway: api implement get secrets removeoverridden
This commit is contained in:
Simone Gotti 2019-09-20 18:46:45 +02:00 committed by GitHub
commit 0172a0aa7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 4 deletions

View File

@ -0,0 +1,42 @@
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package common
import (
csapitypes "agola.io/agola/services/configstore/api/types"
)
func FilterOverriddenSecrets(secrets []*csapitypes.Secret) []*csapitypes.Secret {
secretsMap := map[string]*csapitypes.Secret{}
for _, s := range secrets {
if _, ok := secretsMap[s.Name]; !ok {
secretsMap[s.Name] = s
}
}
filteredSecrets := make([]*csapitypes.Secret, len(secretsMap))
i := 0
// keep the original order
for _, s := range secrets {
if _, ok := secretsMap[s.Name]; !ok {
continue
}
filteredSecrets[i] = s
delete(secretsMap, s.Name)
i++
}
return filteredSecrets
}

View File

@ -18,6 +18,7 @@ import (
"context" "context"
"net/http" "net/http"
"agola.io/agola/internal/services/common"
"agola.io/agola/internal/util" "agola.io/agola/internal/util"
csapitypes "agola.io/agola/services/configstore/api/types" csapitypes "agola.io/agola/services/configstore/api/types"
cstypes "agola.io/agola/services/configstore/types" cstypes "agola.io/agola/services/configstore/types"
@ -30,6 +31,7 @@ type GetSecretsRequest struct {
ParentRef string ParentRef string
Tree bool Tree bool
RemoveOverridden bool
} }
func (h *ActionHandler) GetSecrets(ctx context.Context, req *GetSecretsRequest) ([]*csapitypes.Secret, error) { func (h *ActionHandler) GetSecrets(ctx context.Context, req *GetSecretsRequest) ([]*csapitypes.Secret, error) {
@ -46,6 +48,11 @@ func (h *ActionHandler) GetSecrets(ctx context.Context, req *GetSecretsRequest)
return nil, ErrFromRemote(resp, err) return nil, ErrFromRemote(resp, err)
} }
if req.RemoveOverridden {
// remove overriden secrets
cssecrets = common.FilterOverriddenSecrets(cssecrets)
}
return cssecrets, nil return cssecrets, nil
} }

View File

@ -49,6 +49,7 @@ func (h *SecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
query := r.URL.Query() query := r.URL.Query()
_, tree := query["tree"] _, tree := query["tree"]
_, removeoverridden := query["removeoverridden"]
parentType, parentRef, err := GetConfigTypeRef(r) parentType, parentRef, err := GetConfigTypeRef(r)
if httpError(w, err) { if httpError(w, err) {
@ -60,6 +61,7 @@ func (h *SecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ParentType: parentType, ParentType: parentType,
ParentRef: parentRef, ParentRef: parentRef,
Tree: tree, Tree: tree,
RemoveOverridden: removeoverridden,
} }
cssecrets, err := h.ah.GetSecrets(ctx, areq) cssecrets, err := h.ah.GetSecrets(ctx, areq)
if httpError(w, err) { if httpError(w, err) {