2023-08-15 14:32:43 +02:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package org
|
|
|
|
|
|
|
|
import (
|
2023-09-05 17:21:02 +02:00
|
|
|
"errors"
|
2023-08-15 14:32:43 +02:00
|
|
|
"net/http"
|
|
|
|
|
2023-11-24 04:49:41 +01:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2023-08-22 05:20:34 +02:00
|
|
|
secret_model "code.gitea.io/gitea/models/secret"
|
2023-08-15 14:32:43 +02:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-09-05 17:21:02 +02:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-08-22 05:20:34 +02:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2023-08-15 14:32:43 +02:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2024-02-27 08:12:22 +01:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2023-09-05 17:21:02 +02:00
|
|
|
secret_service "code.gitea.io/gitea/services/secrets"
|
2023-08-15 14:32:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListActionsSecrets list an organization's actions secrets
|
|
|
|
func ListActionsSecrets(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /orgs/{org}/actions/secrets organization orgListActionsSecrets
|
|
|
|
// ---
|
|
|
|
// summary: List an organization's actions secrets
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of the organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
|
|
|
// description: page size of results
|
|
|
|
// type: integer
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/SecretList"
|
2023-09-13 04:37:54 +02:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2023-08-15 14:32:43 +02:00
|
|
|
|
2023-08-22 05:20:34 +02:00
|
|
|
opts := &secret_model.FindSecretsOptions{
|
2023-08-15 14:32:43 +02:00
|
|
|
OwnerID: ctx.Org.Organization.ID,
|
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
|
|
|
}
|
|
|
|
|
2023-11-24 04:49:41 +01:00
|
|
|
secrets, count, err := db.FindAndCount[secret_model.Secret](ctx, opts)
|
2023-08-15 14:32:43 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiSecrets := make([]*api.Secret, len(secrets))
|
|
|
|
for k, v := range secrets {
|
|
|
|
apiSecrets[k] = &api.Secret{
|
|
|
|
Name: v.Name,
|
|
|
|
Created: v.CreatedUnix.AsTime(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.SetTotalCountHeader(count)
|
|
|
|
ctx.JSON(http.StatusOK, apiSecrets)
|
|
|
|
}
|
2023-08-22 05:20:34 +02:00
|
|
|
|
2023-08-28 07:08:19 +02:00
|
|
|
// create or update one secret of the organization
|
2023-08-29 22:54:49 +02:00
|
|
|
func CreateOrUpdateSecret(ctx *context.APIContext) {
|
2023-08-24 04:07:00 +02:00
|
|
|
// swagger:operation PUT /orgs/{org}/actions/secrets/{secretname} organization updateOrgSecret
|
|
|
|
// ---
|
2023-08-28 07:08:19 +02:00
|
|
|
// summary: Create or Update a secret value in an organization
|
2023-08-24 04:07:00 +02:00
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: secretname
|
|
|
|
// in: path
|
|
|
|
// description: name of the secret
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
2023-08-28 07:08:19 +02:00
|
|
|
// "$ref": "#/definitions/CreateOrUpdateSecretOption"
|
2023-08-24 04:07:00 +02:00
|
|
|
// responses:
|
2023-08-28 07:08:19 +02:00
|
|
|
// "201":
|
|
|
|
// description: response when creating a secret
|
2023-08-24 04:07:00 +02:00
|
|
|
// "204":
|
2023-08-28 07:08:19 +02:00
|
|
|
// description: response when updating a secret
|
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
2023-09-05 17:21:02 +02:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
2023-08-28 07:08:19 +02:00
|
|
|
opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
|
2023-09-05 17:21:02 +02:00
|
|
|
|
|
|
|
_, created, err := secret_service.CreateOrUpdateSecret(ctx, ctx.Org.Organization.ID, 0, ctx.Params("secretname"), opt.Data)
|
2023-08-29 22:54:49 +02:00
|
|
|
if err != nil {
|
2023-09-05 17:21:02 +02:00
|
|
|
if errors.Is(err, util.ErrInvalidArgument) {
|
|
|
|
ctx.Error(http.StatusBadRequest, "CreateOrUpdateSecret", err)
|
|
|
|
} else if errors.Is(err, util.ErrNotExist) {
|
|
|
|
ctx.Error(http.StatusNotFound, "CreateOrUpdateSecret", err)
|
|
|
|
} else {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "CreateOrUpdateSecret", err)
|
|
|
|
}
|
2023-08-24 04:07:00 +02:00
|
|
|
return
|
|
|
|
}
|
2023-09-05 17:21:02 +02:00
|
|
|
|
|
|
|
if created {
|
2023-08-29 22:54:49 +02:00
|
|
|
ctx.Status(http.StatusCreated)
|
2023-09-05 17:21:02 +02:00
|
|
|
} else {
|
|
|
|
ctx.Status(http.StatusNoContent)
|
2023-08-24 04:07:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 15:02:49 +02:00
|
|
|
// DeleteSecret delete one secret of the organization
|
|
|
|
func DeleteSecret(ctx *context.APIContext) {
|
2023-08-24 04:07:00 +02:00
|
|
|
// swagger:operation DELETE /orgs/{org}/actions/secrets/{secretname} organization deleteOrgSecret
|
|
|
|
// ---
|
|
|
|
// summary: Delete a secret in an organization
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: secretname
|
|
|
|
// in: path
|
|
|
|
// description: name of the secret
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// description: delete one secret of the organization
|
2023-09-05 17:21:02 +02:00
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
|
|
err := secret_service.DeleteSecretByName(ctx, ctx.Org.Organization.ID, 0, ctx.Params("secretname"))
|
2023-08-24 04:07:00 +02:00
|
|
|
if err != nil {
|
2023-09-05 17:21:02 +02:00
|
|
|
if errors.Is(err, util.ErrInvalidArgument) {
|
|
|
|
ctx.Error(http.StatusBadRequest, "DeleteSecret", err)
|
|
|
|
} else if errors.Is(err, util.ErrNotExist) {
|
|
|
|
ctx.Error(http.StatusNotFound, "DeleteSecret", err)
|
|
|
|
} else {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteSecret", err)
|
|
|
|
}
|
2023-08-24 04:07:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|