-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from csatib02/feat/file-provider
Add provider selection logic, file provider and tests
- Loading branch information
Showing
7 changed files
with
333 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright © 2023 Bank-Vaults Maintainers | ||
// | ||
// 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 main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/bank-vaults/secret-init/provider" | ||
"github.com/bank-vaults/secret-init/provider/file" | ||
) | ||
|
||
func GetEnvironMap() map[string]string { | ||
environ := make(map[string]string, len(os.Environ())) | ||
for _, env := range os.Environ() { | ||
split := strings.SplitN(env, "=", 2) | ||
name := split[0] | ||
value := split[1] | ||
environ[name] = value | ||
} | ||
|
||
return environ | ||
} | ||
|
||
func ExtractPathsFromEnvs(envs map[string]string) []string { | ||
var secretPaths []string | ||
|
||
for _, path := range envs { | ||
if p, path := getProviderPath(path); p != nil { | ||
secretPaths = append(secretPaths, path) | ||
} | ||
} | ||
|
||
return secretPaths | ||
} | ||
|
||
func CreateSecretEnvsFrom(envs map[string]string, secrets []provider.Secret) ([]string, error) { | ||
// Reverse the map so we can match | ||
// the environment variable key to the secret | ||
// by using the secret path | ||
reversedEnvs := make(map[string]string) | ||
for envKey, path := range envs { | ||
if p, path := getProviderPath(path); p != nil { | ||
reversedEnvs[path] = envKey | ||
} | ||
} | ||
|
||
var secretsEnv []string | ||
for _, secret := range secrets { | ||
path := secret.Path | ||
value := secret.Value | ||
key, ok := reversedEnvs[path] | ||
if !ok { | ||
return nil, fmt.Errorf("failed to find environment variable key for secret path: %s", path) | ||
} | ||
secretsEnv = append(secretsEnv, fmt.Sprintf("%s=%s", key, value)) | ||
} | ||
|
||
return secretsEnv, nil | ||
} | ||
|
||
// Returns the detected provider name and path with removed prefix | ||
func getProviderPath(path string) (*string, string) { | ||
if strings.HasPrefix(path, "file:") { | ||
var fileProviderName = file.ProviderName | ||
return &fileProviderName, strings.TrimPrefix(path, "file:") | ||
} | ||
|
||
return nil, path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright © 2023 Bank-Vaults Maintainers | ||
// | ||
// 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 file | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/fs" | ||
"strings" | ||
|
||
"github.com/bank-vaults/secret-init/provider" | ||
) | ||
|
||
const ProviderName = "file" | ||
|
||
type Provider struct { | ||
fs fs.FS | ||
} | ||
|
||
func NewProvider(fs fs.FS) (provider.Provider, error) { | ||
if fs == nil { | ||
return nil, fmt.Errorf("file system is nil") | ||
} | ||
|
||
return &Provider{fs: fs}, nil | ||
} | ||
|
||
func (p *Provider) LoadSecrets(_ context.Context, paths []string) ([]provider.Secret, error) { | ||
var secrets []provider.Secret | ||
|
||
for _, path := range paths { | ||
secret, err := p.getSecretFromFile(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get secret from file: %w", err) | ||
} | ||
|
||
secrets = append(secrets, provider.Secret{ | ||
Path: path, | ||
Value: secret, | ||
}) | ||
} | ||
|
||
return secrets, nil | ||
} | ||
|
||
func (p *Provider) getSecretFromFile(filepath string) (string, error) { | ||
filepath = strings.TrimLeft(filepath, "/") | ||
content, err := fs.ReadFile(p.fs, filepath) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to read file: %w", err) | ||
} | ||
|
||
return string(content), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright © 2023 Bank-Vaults Maintainers | ||
// | ||
// 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 file | ||
|
||
import ( | ||
"context" | ||
"io/fs" | ||
"testing" | ||
"testing/fstest" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/bank-vaults/secret-init/provider" | ||
) | ||
|
||
func TestNewProvider(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
fs fs.FS | ||
wantErr bool | ||
wantType bool | ||
}{ | ||
{ | ||
name: "Valid file system", | ||
fs: fstest.MapFS{ | ||
"test/secrets/sqlpass.txt": &fstest.MapFile{Data: []byte("3xtr3ms3cr3t")}, | ||
"test/secrets/awsaccess.txt": &fstest.MapFile{Data: []byte("s3cr3t")}, | ||
"test/secrets/awsid.txt": &fstest.MapFile{Data: []byte("secretId")}, | ||
}, | ||
wantErr: false, | ||
wantType: true, | ||
}, | ||
{ | ||
name: "Nil file system", | ||
fs: nil, | ||
wantErr: true, | ||
wantType: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
ttp := tt | ||
t.Run(ttp.name, func(t *testing.T) { | ||
prov, err := NewProvider(ttp.fs) | ||
if (err != nil) != ttp.wantErr { | ||
t.Fatalf("NewProvider() error = %v, wantErr %v", err, ttp.wantErr) | ||
return | ||
} | ||
// Use type assertion to check if the provider is of the correct type | ||
_, ok := prov.(*Provider) | ||
if ok != ttp.wantType { | ||
t.Fatalf("NewProvider() = %v, wantType %v", ok, ttp.wantType) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestLoadSecrets(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
fs fs.FS | ||
paths []string | ||
wantErr bool | ||
wantData []provider.Secret | ||
}{ | ||
{ | ||
name: "Load secrets successfully", | ||
fs: fstest.MapFS{ | ||
"test/secrets/sqlpass.txt": &fstest.MapFile{Data: []byte("3xtr3ms3cr3t")}, | ||
"test/secrets/awsaccess.txt": &fstest.MapFile{Data: []byte("s3cr3t")}, | ||
"test/secrets/awsid.txt": &fstest.MapFile{Data: []byte("secretId")}, | ||
}, | ||
paths: []string{ | ||
"test/secrets/sqlpass.txt", | ||
"test/secrets/awsaccess.txt", | ||
"test/secrets/awsid.txt", | ||
}, | ||
wantErr: false, | ||
wantData: []provider.Secret{ | ||
{Path: "test/secrets/sqlpass.txt", Value: "3xtr3ms3cr3t"}, | ||
{Path: "test/secrets/awsaccess.txt", Value: "s3cr3t"}, | ||
{Path: "test/secrets/awsid.txt", Value: "secretId"}, | ||
}, | ||
}, | ||
{ | ||
name: "Fail to load secrets due to invalid path", | ||
fs: fstest.MapFS{ | ||
"test/secrets/sqlpass.txt": &fstest.MapFile{Data: []byte("3xtr3ms3cr3t")}, | ||
"test/secrets/awsaccess.txt": &fstest.MapFile{Data: []byte("s3cr3t")}, | ||
"test/secrets/awsid.txt": &fstest.MapFile{Data: []byte("secretId")}, | ||
}, | ||
paths: []string{ | ||
"test/secrets/mistake/sqlpass.txt", | ||
"test/secrets/mistake/awsaccess.txt", | ||
"test/secrets/mistake/awsid.txt", | ||
}, | ||
wantErr: true, | ||
wantData: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
ttp := tt | ||
t.Run(ttp.name, func(t *testing.T) { | ||
provider, err := NewProvider(ttp.fs) | ||
if assert.NoError(t, err, "Unexpected error") { | ||
secrets, err := provider.LoadSecrets(context.Background(), ttp.paths) | ||
assert.Equal(t, ttp.wantErr, err != nil, "Unexpected error status") | ||
assert.ElementsMatch(t, ttp.wantData, secrets, "Unexpected secrets loaded") | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.