2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-04-11 17:06:45 +00:00
|
|
|
package secrets
|
|
|
|
|
|
|
|
import (
|
2021-12-11 12:15:04 +00:00
|
|
|
"context"
|
|
|
|
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2017-04-11 17:06:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type builtin struct {
|
2021-12-11 12:15:04 +00:00
|
|
|
context.Context
|
2017-04-11 17:06:45 +00:00
|
|
|
store model.SecretStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new local secret service.
|
2021-12-11 12:15:04 +00:00
|
|
|
func New(ctx context.Context, store model.SecretStore) model.SecretService {
|
|
|
|
return &builtin{store: store, Context: ctx}
|
2017-04-11 17:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builtin) SecretFind(repo *model.Repo, name string) (*model.Secret, error) {
|
|
|
|
return b.store.SecretFind(repo, name)
|
|
|
|
}
|
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
func (b *builtin) SecretList(repo *model.Repo, p *model.ListOptions) ([]*model.Secret, error) {
|
|
|
|
return b.store.SecretList(repo, false, p)
|
2017-04-11 17:06:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
func (b *builtin) SecretListPipeline(repo *model.Repo, _ *model.Pipeline, p *model.ListOptions) ([]*model.Secret, error) {
|
|
|
|
s, err := b.store.SecretList(repo, true, p)
|
2022-08-14 11:48:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return only secrets with unique name
|
|
|
|
// Priority order in case of duplicate names are repository, user/organization, global
|
|
|
|
secrets := make([]*model.Secret, 0, len(s))
|
|
|
|
uniq := make(map[string]struct{})
|
|
|
|
for _, cond := range []struct {
|
|
|
|
Global bool
|
|
|
|
Organization bool
|
|
|
|
}{
|
|
|
|
{},
|
|
|
|
{Organization: true},
|
|
|
|
{Global: true},
|
|
|
|
} {
|
|
|
|
for _, secret := range s {
|
2023-10-16 21:39:55 +00:00
|
|
|
if secret.Global() != cond.Global || secret.Organization() != cond.Organization {
|
2022-08-14 11:48:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := uniq[secret.Name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
uniq[secret.Name] = struct{}{}
|
|
|
|
secrets = append(secrets, secret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return secrets, nil
|
2017-05-12 10:30:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-18 19:35:27 +00:00
|
|
|
func (b *builtin) SecretCreate(_ *model.Repo, in *model.Secret) error {
|
2017-04-11 17:06:45 +00:00
|
|
|
return b.store.SecretCreate(in)
|
|
|
|
}
|
|
|
|
|
2023-03-18 19:35:27 +00:00
|
|
|
func (b *builtin) SecretUpdate(_ *model.Repo, in *model.Secret) error {
|
2017-04-11 17:06:45 +00:00
|
|
|
return b.store.SecretUpdate(in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builtin) SecretDelete(repo *model.Repo, name string) error {
|
|
|
|
secret, err := b.store.SecretFind(repo, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.store.SecretDelete(secret)
|
|
|
|
}
|
2022-08-14 11:48:53 +00:00
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
func (b *builtin) OrgSecretFind(owner int64, name string) (*model.Secret, error) {
|
2022-08-14 11:48:53 +00:00
|
|
|
return b.store.OrgSecretFind(owner, name)
|
|
|
|
}
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
func (b *builtin) OrgSecretList(owner int64, p *model.ListOptions) ([]*model.Secret, error) {
|
2023-04-30 01:40:13 +00:00
|
|
|
return b.store.OrgSecretList(owner, p)
|
2022-08-14 11:48:53 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
func (b *builtin) OrgSecretCreate(_ int64, in *model.Secret) error {
|
2022-08-14 11:48:53 +00:00
|
|
|
return b.store.SecretCreate(in)
|
|
|
|
}
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
func (b *builtin) OrgSecretUpdate(_ int64, in *model.Secret) error {
|
2022-08-14 11:48:53 +00:00
|
|
|
return b.store.SecretUpdate(in)
|
|
|
|
}
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
func (b *builtin) OrgSecretDelete(owner int64, name string) error {
|
2022-08-14 11:48:53 +00:00
|
|
|
secret, err := b.store.OrgSecretFind(owner, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.store.SecretDelete(secret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builtin) GlobalSecretFind(owner string) (*model.Secret, error) {
|
|
|
|
return b.store.GlobalSecretFind(owner)
|
|
|
|
}
|
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
func (b *builtin) GlobalSecretList(p *model.ListOptions) ([]*model.Secret, error) {
|
|
|
|
return b.store.GlobalSecretList(p)
|
2022-08-14 11:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builtin) GlobalSecretCreate(in *model.Secret) error {
|
|
|
|
return b.store.SecretCreate(in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builtin) GlobalSecretUpdate(in *model.Secret) error {
|
|
|
|
return b.store.SecretUpdate(in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builtin) GlobalSecretDelete(name string) error {
|
|
|
|
secret, err := b.store.GlobalSecretFind(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.store.SecretDelete(secret)
|
|
|
|
}
|