-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds env secret provider (#38)
- Loading branch information
Showing
4 changed files
with
97 additions
and
4 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
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,32 @@ | ||
package providers | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
type EnvClient struct { | ||
logger *slog.Logger | ||
} | ||
|
||
func NewEnvClient(logger *slog.Logger) (*EnvClient, error) { | ||
return &EnvClient{ | ||
logger: logger, | ||
}, nil | ||
} | ||
|
||
func (c *EnvClient) Get(key string) (string, error) { | ||
c.logger.Debug("Getting secret from environment variable", "key", key) | ||
|
||
secret, ok := os.LookupEnv(key) | ||
if !ok { | ||
return "", fmt.Errorf("enviroment variable %s not found", key) | ||
} | ||
|
||
return secret, nil | ||
} | ||
|
||
func (c *EnvClient) Set(key, value string) (string, error) { | ||
panic("not implemented") | ||
} |
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,57 @@ | ||
package providers | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/input-output-hk/catalyst-forge/tools/pkg/testutils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEnvClientGet(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
key string | ||
env map[string]string | ||
expect string | ||
expectErr bool | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "simple", | ||
key: "FOO", | ||
env: map[string]string{ | ||
"FOO": "secret", | ||
}, | ||
expect: "secret", | ||
expectErr: false, | ||
expectedErr: "", | ||
}, | ||
{ | ||
name: "secret not found", | ||
key: "BAR", | ||
env: map[string]string{}, | ||
expect: "", | ||
expectErr: true, | ||
expectedErr: "enviroment variable BAR not found", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
client := &EnvClient{ | ||
logger: testutils.NewNoopLogger(), | ||
} | ||
|
||
for k, v := range tt.env { | ||
_ = os.Setenv(k, v) | ||
} | ||
|
||
got, err := client.Get(tt.key) | ||
if testutils.AssertError(t, err, tt.expectErr, tt.expectedErr) { | ||
return | ||
} | ||
assert.Equal(t, tt.expect, got) | ||
}) | ||
} | ||
} |