|
| 1 | +// Copyright 2022 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package secret |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "regexp" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/models/db" |
| 13 | + secret_module "code.gitea.io/gitea/modules/secret" |
| 14 | + "code.gitea.io/gitea/modules/setting" |
| 15 | + "code.gitea.io/gitea/modules/timeutil" |
| 16 | + "code.gitea.io/gitea/modules/util" |
| 17 | + |
| 18 | + "xorm.io/builder" |
| 19 | +) |
| 20 | + |
| 21 | +type ErrSecretInvalidValue struct { |
| 22 | + Name *string |
| 23 | + Data *string |
| 24 | +} |
| 25 | + |
| 26 | +func (err ErrSecretInvalidValue) Error() string { |
| 27 | + if err.Name != nil { |
| 28 | + return fmt.Sprintf("secret name %q is invalid", *err.Name) |
| 29 | + } |
| 30 | + if err.Data != nil { |
| 31 | + return fmt.Sprintf("secret data %q is invalid", *err.Data) |
| 32 | + } |
| 33 | + return util.ErrInvalidArgument.Error() |
| 34 | +} |
| 35 | + |
| 36 | +func (err ErrSecretInvalidValue) Unwrap() error { |
| 37 | + return util.ErrInvalidArgument |
| 38 | +} |
| 39 | + |
| 40 | +// Secret represents a secret |
| 41 | +type Secret struct { |
| 42 | + ID int64 |
| 43 | + OwnerID int64 `xorm:"INDEX UNIQUE(owner_repo_name) NOT NULL"` |
| 44 | + RepoID int64 `xorm:"INDEX UNIQUE(owner_repo_name) NOT NULL DEFAULT 0"` |
| 45 | + Name string `xorm:"UNIQUE(owner_repo_name) NOT NULL"` |
| 46 | + Data string `xorm:"LONGTEXT"` // encrypted data |
| 47 | + CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"` |
| 48 | +} |
| 49 | + |
| 50 | +// newSecret Creates a new already encrypted secret |
| 51 | +func newSecret(ownerID, repoID int64, name, data string) *Secret { |
| 52 | + return &Secret{ |
| 53 | + OwnerID: ownerID, |
| 54 | + RepoID: repoID, |
| 55 | + Name: strings.ToUpper(name), |
| 56 | + Data: data, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// InsertEncryptedSecret Creates, encrypts, and validates a new secret with yet unencrypted data and insert into database |
| 61 | +func InsertEncryptedSecret(ctx context.Context, ownerID, repoID int64, name, data string) (*Secret, error) { |
| 62 | + encrypted, err := secret_module.EncryptSecret(setting.SecretKey, strings.TrimSpace(data)) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + secret := newSecret(ownerID, repoID, name, encrypted) |
| 67 | + if err := secret.Validate(); err != nil { |
| 68 | + return secret, err |
| 69 | + } |
| 70 | + return secret, db.Insert(ctx, secret) |
| 71 | +} |
| 72 | + |
| 73 | +func init() { |
| 74 | + db.RegisterModel(new(Secret)) |
| 75 | +} |
| 76 | + |
| 77 | +var ( |
| 78 | + secretNameReg = regexp.MustCompile("^[A-Z_][A-Z0-9_]*$") |
| 79 | + forbiddenSecretPrefixReg = regexp.MustCompile("^GIT(EA|HUB)_") |
| 80 | +) |
| 81 | + |
| 82 | +// Validate validates the required fields and formats. |
| 83 | +func (s *Secret) Validate() error { |
| 84 | + switch { |
| 85 | + case len(s.Name) == 0 || len(s.Name) > 50: |
| 86 | + return ErrSecretInvalidValue{Name: &s.Name} |
| 87 | + case len(s.Data) == 0: |
| 88 | + return ErrSecretInvalidValue{Data: &s.Data} |
| 89 | + case !secretNameReg.MatchString(s.Name) || |
| 90 | + forbiddenSecretPrefixReg.MatchString(s.Name): |
| 91 | + return ErrSecretInvalidValue{Name: &s.Name} |
| 92 | + default: |
| 93 | + return nil |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +type FindSecretsOptions struct { |
| 98 | + db.ListOptions |
| 99 | + OwnerID int64 |
| 100 | + RepoID int64 |
| 101 | +} |
| 102 | + |
| 103 | +func (opts *FindSecretsOptions) toConds() builder.Cond { |
| 104 | + cond := builder.NewCond() |
| 105 | + if opts.OwnerID > 0 { |
| 106 | + cond = cond.And(builder.Eq{"owner_id": opts.OwnerID}) |
| 107 | + } |
| 108 | + if opts.RepoID > 0 { |
| 109 | + cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) |
| 110 | + } |
| 111 | + |
| 112 | + return cond |
| 113 | +} |
| 114 | + |
| 115 | +func FindSecrets(ctx context.Context, opts FindSecretsOptions) ([]*Secret, error) { |
| 116 | + var secrets []*Secret |
| 117 | + sess := db.GetEngine(ctx) |
| 118 | + if opts.PageSize != 0 { |
| 119 | + sess = db.SetSessionPagination(sess, &opts.ListOptions) |
| 120 | + } |
| 121 | + return secrets, sess. |
| 122 | + Where(opts.toConds()). |
| 123 | + Find(&secrets) |
| 124 | +} |
0 commit comments