Skip to content

Commit

Permalink
Merge pull request containers#9583 from ashley-cui/sec
Browse files Browse the repository at this point in the history
Add version field to secret compat list/inspect api
  • Loading branch information
openshift-merge-robot authored Mar 3, 2021
2 parents 5fd8a84 + 9391bfc commit 259bb5f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
32 changes: 30 additions & 2 deletions pkg/api/handlers/compat/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,21 @@ func ListSecrets(w http.ResponseWriter, r *http.Request) {
utils.InternalServerError(w, err)
return
}
utils.WriteResponse(w, http.StatusOK, reports)
if utils.IsLibpodRequest(r) {
utils.WriteResponse(w, http.StatusOK, reports)
return
}
// Docker compat expects a version field that increments when the secret is updated
// We currently can't update a secret, so we default the version to 1
compatReports := make([]entities.SecretInfoReportCompat, 0, len(reports))
for _, report := range reports {
compatRep := entities.SecretInfoReportCompat{
SecretInfoReport: *report,
Version: entities.SecretVersion{Index: 1},
}
compatReports = append(compatReports, compatRep)
}
utils.WriteResponse(w, http.StatusOK, compatReports)
}

func InspectSecret(w http.ResponseWriter, r *http.Request) {
Expand All @@ -59,7 +73,21 @@ func InspectSecret(w http.ResponseWriter, r *http.Request) {
utils.SecretNotFound(w, name, errs[0])
return
}
utils.WriteResponse(w, http.StatusOK, reports[0])
if len(reports) < 1 {
utils.InternalServerError(w, err)
return
}
if utils.IsLibpodRequest(r) {
utils.WriteResponse(w, http.StatusOK, reports[0])
return
}
// Docker compat expects a version field that increments when the secret is updated
// We currently can't update a secret, so we default the version to 1
compatReport := entities.SecretInfoReportCompat{
SecretInfoReport: *reports[0],
Version: entities.SecretVersion{Index: 1},
}
utils.WriteResponse(w, http.StatusOK, compatReport)
}

func RemoveSecret(w http.ResponseWriter, r *http.Request) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/server/register_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (s *APIServer) registerSecretHandlers(r *mux.Router) error {
// parameters:
// responses:
// '200':
// "$ref": "#/responses/SecretListResponse"
// "$ref": "#/responses/SecretListCompatResponse"
// '500':
// "$ref": "#/responses/InternalError"
r.Handle(VersionedPath("/secrets"), s.APIHandler(compat.ListSecrets)).Methods(http.MethodGet)
Expand Down Expand Up @@ -158,7 +158,7 @@ func (s *APIServer) registerSecretHandlers(r *mux.Router) error {
// - application/json
// responses:
// '200':
// "$ref": "#/responses/SecretInspectResponse"
// "$ref": "#/responses/SecretInspectCompatResponse"
// '404':
// "$ref": "#/responses/NoSuchSecret"
// '500':
Expand Down
23 changes: 23 additions & 0 deletions pkg/domain/entities/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ type SecretInfoReport struct {
Spec SecretSpec
}

type SecretInfoReportCompat struct {
SecretInfoReport
Version SecretVersion
}

type SecretVersion struct {
Index int
}

type SecretSpec struct {
Name string
Driver SecretDriverSpec
Expand Down Expand Up @@ -78,13 +87,27 @@ type SwagSecretListResponse struct {
Body []*SecretInfoReport
}

// Secret list response
// swagger:response SecretListCompatResponse
type SwagSecretListCompatResponse struct {
// in:body
Body []*SecretInfoReportCompat
}

// Secret inspect response
// swagger:response SecretInspectResponse
type SwagSecretInspectResponse struct {
// in:body
Body SecretInfoReport
}

// Secret inspect compat
// swagger:response SecretInspectCompatResponse
type SwagSecretInspectCompatResponse struct {
// in:body
Body SecretInfoReportCompat
}

// No such secret
// swagger:response NoSuchSecret
type SwagErrNoSuchSecret struct {
Expand Down
11 changes: 7 additions & 4 deletions test/apiv2/50-secrets.at
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ t POST secrets/create '"Name":"mysecret","Data":"c2VjcmV0","Labels":{"fail":"fai
t POST secrets/create '"Name":"mysecret","Data":"c2VjcmV0"' 409

# secret inspect
t GET secrets/mysecret 200\
.Spec.Name=mysecret
t GET secrets/mysecret 200 \
.Spec.Name=mysecret \
.Version.Index=1

# secret inspect non-existent secret
t GET secrets/bogus 404

# secret list
t GET secrets 200\
length=1
t GET secrets 200 \
length=1 \
.[0].Spec.Name=mysecret \
.[0].Version.Index=1

# secret list unsupported filters
t GET secrets?filters='{"name":["foo1"]}' 400
Expand Down

0 comments on commit 259bb5f

Please sign in to comment.