Skip to content

Commit 8034027

Browse files
committed
feat: add functions and structs for managing organization secrets
- Add `ChangeSecret` function to modify org or user repo secret - Add `DeleteSecret` function to delete secret from an organization - Add `UpdateSecretOption` struct for updating secret options - Add `UpdateOrgSecret` function to update a secret in an organization - Add `DeleteOrgSecret` function to delete a secret in an organization Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 23addde commit 8034027

File tree

6 files changed

+240
-2
lines changed

6 files changed

+240
-2
lines changed

models/secret/secret.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package secret
66
import (
77
"context"
88
"errors"
9+
"fmt"
910
"strings"
1011

1112
"code.gitea.io/gitea/models/db"
@@ -93,3 +94,48 @@ func FindSecrets(ctx context.Context, opts FindSecretsOptions) ([]*Secret, error
9394
func CountSecrets(ctx context.Context, opts *FindSecretsOptions) (int64, error) {
9495
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(Secret))
9596
}
97+
98+
// ChangeSecret changes org or user reop secret.
99+
func ChangeSecret(ctx context.Context, orgID, repoID int64, name, data string) error {
100+
sc := new(Secret)
101+
has, err := db.GetEngine(ctx).
102+
Where("owner_id=?", orgID).
103+
And("repo_id=?", repoID).
104+
And("name=?", strings.ToUpper(name)).
105+
Get(sc)
106+
if err != nil {
107+
return err
108+
} else if !has {
109+
return errors.New("secret not found")
110+
}
111+
112+
encrypted, err := secret_module.EncryptSecret(setting.SecretKey, data)
113+
if err != nil {
114+
return err
115+
}
116+
117+
sc.Data = encrypted
118+
_, err = db.GetEngine(ctx).ID(sc.ID).Cols("data").Update(sc)
119+
return err
120+
}
121+
122+
// DeleteSecret deletes secret from an organization.
123+
func DeleteSecret(ctx context.Context, orgID, repoID int64, name string) error {
124+
sc := new(Secret)
125+
has, err := db.GetEngine(ctx).
126+
Where("owner_id=?", orgID).
127+
And("repo_id=?", repoID).
128+
And("name=?", strings.ToUpper(name)).
129+
Get(sc)
130+
if err != nil {
131+
return err
132+
} else if !has {
133+
return errors.New("secret not found")
134+
}
135+
136+
if _, err := db.GetEngine(ctx).ID(sc.ID).Delete(new(Secret)); err != nil {
137+
return fmt.Errorf("Delete: %w", err)
138+
}
139+
140+
return nil
141+
}

modules/structs/secret.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,12 @@ type CreateSecretOption struct {
2525
// Data of the secret to create
2626
Data string `json:"data" binding:"Required"`
2727
}
28+
29+
// UpdateSecretOption options when updating secret
30+
// swagger:model
31+
type UpdateSecretOption struct {
32+
// Data of the secret to create
33+
//
34+
// required: true
35+
Data string `json:"data" binding:"Required"`
36+
}

routers/api/v1/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,9 @@ func Routes() *web.Route {
13011301
m.Group("/actions/secrets", func() {
13021302
m.Get("", reqToken(), reqOrgOwnership(), org.ListActionsSecrets)
13031303
m.Post("", reqToken(), reqOrgOwnership(), bind(api.CreateSecretOption{}), org.CreateOrgSecret)
1304+
m.Combo("/{secretname}").
1305+
Put(reqToken(), reqOrgOwnership(), bind(api.UpdateSecretOption{}), org.UpdateOrgSecret).
1306+
Delete(reqToken(), reqOrgOwnership(), org.DeleteOrgSecret)
13041307
})
13051308
m.Group("/public_members", func() {
13061309
m.Get("", org.ListPublicMembers)

routers/api/v1/org/action.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,82 @@ func CreateOrgSecret(ctx *context.APIContext) {
113113

114114
ctx.JSON(http.StatusCreated, convert.ToSecret(s))
115115
}
116+
117+
// UpdateOrgSecret update one secret of the organization
118+
func UpdateOrgSecret(ctx *context.APIContext) {
119+
// swagger:operation PUT /orgs/{org}/actions/secrets/{secretname} organization updateOrgSecret
120+
// ---
121+
// summary: Update a secret in an organization
122+
// consumes:
123+
// - application/json
124+
// produces:
125+
// - application/json
126+
// parameters:
127+
// - name: org
128+
// in: path
129+
// description: name of organization
130+
// type: string
131+
// required: true
132+
// - name: secretname
133+
// in: path
134+
// description: name of the secret
135+
// type: string
136+
// required: true
137+
// - name: body
138+
// in: body
139+
// schema:
140+
// "$ref": "#/definitions/UpdateSecretOption"
141+
// responses:
142+
// "204":
143+
// description: membership publicized
144+
// "403":
145+
// "$ref": "#/responses/forbidden"
146+
secretName := ctx.Params(":secretname")
147+
opt := web.GetForm(ctx).(*api.UpdateSecretOption)
148+
err := secret_model.ChangeSecret(
149+
ctx, ctx.Org.Organization.ID, 0, secretName, opt.Data,
150+
)
151+
if err != nil {
152+
ctx.Error(http.StatusInternalServerError, "ChangeSecret", err)
153+
return
154+
}
155+
156+
ctx.Status(http.StatusNoContent)
157+
}
158+
159+
// DeleteOrgSecret delete one secret of the organization
160+
func DeleteOrgSecret(ctx *context.APIContext) {
161+
// swagger:operation DELETE /orgs/{org}/actions/secrets/{secretname} organization deleteOrgSecret
162+
// ---
163+
// summary: Delete a secret in an organization
164+
// consumes:
165+
// - application/json
166+
// produces:
167+
// - application/json
168+
// parameters:
169+
// - name: org
170+
// in: path
171+
// description: name of organization
172+
// type: string
173+
// required: true
174+
// - name: secretname
175+
// in: path
176+
// description: name of the secret
177+
// type: string
178+
// required: true
179+
// responses:
180+
// "204":
181+
// description: membership publicized
182+
// "403":
183+
// "$ref": "#/responses/forbidden"
184+
secretName := ctx.Params(":secretname")
185+
err := secret_model.DeleteSecret(
186+
ctx, ctx.Org.Organization.ID, 0, secretName,
187+
)
188+
if err != nil {
189+
ctx.Error(http.StatusInternalServerError, "DeleteSecret", err)
190+
return
191+
}
192+
193+
ctx.Status(http.StatusNoContent)
194+
}

routers/api/v1/swagger/options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,7 @@ type swaggerParameterBodies struct {
190190

191191
// in:body
192192
CreateSecretOption api.CreateSecretOption
193+
194+
// in:body
195+
UpdateSecretOption api.UpdateSecretOption
193196
}

templates/swagger/v1_json.tmpl

Lines changed: 100 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)