2023-01-12 19:59:07 +00:00
|
|
|
// Copyright 2023 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.
|
|
|
|
|
|
|
|
package encryption
|
|
|
|
|
|
|
|
import (
|
2023-02-01 23:08:02 +00:00
|
|
|
"errors"
|
2023-01-12 19:59:07 +00:00
|
|
|
"fmt"
|
|
|
|
|
2024-07-17 23:26:35 +00:00
|
|
|
"github.com/urfave/cli/v3"
|
2023-01-12 19:59:07 +00:00
|
|
|
|
2024-12-22 09:44:34 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/server/services/encryption/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/server/store"
|
2023-01-12 19:59:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type aesConfiguration struct {
|
|
|
|
password string
|
|
|
|
store store.Store
|
2024-02-11 17:42:33 +00:00
|
|
|
clients []types.EncryptionClient
|
2023-01-12 19:59:07 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 23:26:35 +00:00
|
|
|
func newAES(c *cli.Command, s store.Store) types.EncryptionServiceBuilder {
|
|
|
|
key := c.String(rawKeyConfigFlag)
|
2023-01-12 19:59:07 +00:00
|
|
|
return &aesConfiguration{key, s, nil}
|
|
|
|
}
|
|
|
|
|
2024-02-11 17:42:33 +00:00
|
|
|
func (c aesConfiguration) WithClients(clients []types.EncryptionClient) types.EncryptionServiceBuilder {
|
2023-01-12 19:59:07 +00:00
|
|
|
c.clients = clients
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2024-02-11 17:42:33 +00:00
|
|
|
func (c aesConfiguration) Build() (types.EncryptionService, error) {
|
2023-01-12 19:59:07 +00:00
|
|
|
svc := &aesEncryptionService{
|
|
|
|
cipher: nil,
|
|
|
|
store: c.store,
|
|
|
|
clients: c.clients,
|
|
|
|
}
|
|
|
|
err := svc.initClients()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(errTemplateFailedInitializingClients, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = svc.loadCipher(c.password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(errTemplateAesFailedLoadingCipher, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = svc.validateKey()
|
2023-02-01 23:08:02 +00:00
|
|
|
if errors.Is(err, errEncryptionNotEnabled) {
|
2023-01-12 19:59:07 +00:00
|
|
|
err = svc.enable()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(errTemplateFailedValidatingKey, err)
|
|
|
|
}
|
|
|
|
return svc, nil
|
|
|
|
}
|