Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Vault provider #35

Merged
merged 25 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2bba5c5
feat(file provider): Add config, adjust tests
csatib02 Dec 15, 2023
a3085a4
feat(vault provider config): Add config
csatib02 Dec 15, 2023
8850c77
fix(vault provider config): Add lience
csatib02 Dec 15, 2023
a27bcef
feat(vault provider): Add Vault provider
csatib02 Dec 17, 2023
1820fed
fix(vault provider): linter
csatib02 Dec 17, 2023
7256c31
feat(vault provider): Add sanitization
csatib02 Dec 19, 2023
27223b3
feat(vault provider): Implementation finished
csatib02 Dec 20, 2023
bed0ba8
fix(go.mod) update dependency
csatib02 Dec 20, 2023
a330483
fix(env.go) single provider workaround
csatib02 Dec 20, 2023
fb2688c
fix(env.go, main.go) vault provider secret loading
csatib02 Dec 20, 2023
46216ef
feat(vault provider config test): Add config test
csatib02 Dec 20, 2023
4f7ae51
feat(vault provider tests): Add more tests, and minor fixes
csatib02 Dec 21, 2023
b0f4197
fix(vault provider tests): Minor fixes
csatib02 Dec 21, 2023
21cff0e
fix: fix remarks
csatib02 Jan 11, 2024
cc25360
feat(common): add common package for env-vars
csatib02 Jan 18, 2024
4af1b5b
fix(tests): improve tests
csatib02 Jan 18, 2024
f002f26
chore: simplify and improve code
csatib02 Jan 18, 2024
cb9a1c8
feat(pkg: args, config, envstore): add new package, factor out main, …
csatib02 Jan 22, 2024
fb24d3c
chore(configs): minor changes
csatib02 Jan 23, 2024
573ecf5
fix(tests): improve tests
csatib02 Jan 23, 2024
1b9480f
fix(file provider): minor fixes
csatib02 Jan 23, 2024
490bec6
fix(vault provider): minor fixes
csatib02 Jan 23, 2024
08d3e1b
fix(main, common): minor fixes, moved back args.go and env_store.go t…
csatib02 Jan 23, 2024
6275cb8
fix(vault test): add test for vault:login
csatib02 Jan 23, 2024
c4d38a3
chore: minor fixes
csatib02 Jan 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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/exec"
)

// ExtractEntrypoint extracts entrypoint data in the form of binary path and its arguments from the
// os.Args. Note that the path to the binary will be returned as the first element.
func ExtractEntrypoint(args []string) (string, []string, error) {
if len(args) <= 1 {
return "", nil, fmt.Errorf("no args provided")
}

binaryPath, err := exec.LookPath(args[1])
if err != nil {
return "", nil, fmt.Errorf("binary %s not found", args[1])
}

var binaryArgs []string
if len(args) >= 2 {
binaryArgs = args[2:] // returns the arguments for the binary
}

return binaryPath, binaryArgs, nil
}
68 changes: 68 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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"
"testing"

"github.com/stretchr/testify/assert"
)

func TestExtractEntrypoint(t *testing.T) {
tests := []struct {
name string
args []string
expectedBinaryPath string
expectedBinaryArgs []string
err error
}{
{
name: "Valid case with one argument",
args: []string{"secret-init", "env"},
expectedBinaryPath: "/usr/bin/env",
expectedBinaryArgs: []string{},
},
{
name: "Valid case with more than two arguments",
args: []string{"secret-init", "env", "|", "grep", "secrets"},
expectedBinaryPath: "/usr/bin/env",
expectedBinaryArgs: []string{"|", "grep", "secrets"},
},
{
name: "Invalid case - no arguments",
args: []string{"secret-init"},
err: fmt.Errorf("no args provided"),
},
{
name: "Invalid case - binary not found",
args: []string{"secret-init", "nonexistentBinary"},
err: fmt.Errorf("binary nonexistentBinary not found"),
},
}

for _, tt := range tests {
ttp := tt
t.Run(ttp.name, func(t *testing.T) {
binaryPath, binaryArgs, err := ExtractEntrypoint(ttp.args)
if err != nil {
assert.EqualError(t, ttp.err, err.Error(), "Unexpected error message")
} else {
assert.Equal(t, ttp.expectedBinaryPath, binaryPath, "Unexpected binary path")
assert.Equal(t, ttp.expectedBinaryArgs, binaryArgs, "Unexpected binary args")
}
})
}
}
51 changes: 51 additions & 0 deletions common/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 common

import (
"os"
"time"

"github.com/spf13/cast"
)

const (
LogLevelEnv = "SECRET_INIT_LOG_LEVEL"
JSONLogEnv = "SECRET_INIT_JSON_LOG"
LogServerEnv = "SECRET_INIT_LOG_SERVER"
DaemonEnv = "SECRET_INIT_DAEMON"
DelayEnv = "SECRET_INIT_DELAY"
ProviderEnv = "SECRET_INIT_PROVIDER"
)

type Config struct {
LogLevel string `json:"log_level"`
JSONLog bool `json:"json_log"`
LogServer string `json:"log_server"`
Daemon bool `json:"daemon"`
Delay time.Duration `json:"delay"`
Provider string `json:"provider"`
}

func LoadConfig() (*Config, error) {
return &Config{
LogLevel: os.Getenv(LogLevelEnv),
JSONLog: cast.ToBool(os.Getenv(JSONLogEnv)),
LogServer: os.Getenv(LogServerEnv),
Daemon: cast.ToBool(os.Getenv(DaemonEnv)),
Delay: cast.ToDuration(os.Getenv(DelayEnv)),
Provider: os.Getenv(ProviderEnv),
}, nil
}
67 changes: 67 additions & 0 deletions common/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 common

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfig(t *testing.T) {
tests := []struct {
name string
env map[string]string
wantConfig *Config
}{
{
name: "Valid configuration",
env: map[string]string{
LogLevelEnv: "debug",
JSONLogEnv: "true",
LogServerEnv: "",
DaemonEnv: "true",
ProviderEnv: "vault",
},
wantConfig: &Config{
LogLevel: "debug",
JSONLog: true,
LogServer: "",
Daemon: true,
Provider: "vault",
},
},
}

for _, tt := range tests {
ttp := tt
t.Run(ttp.name, func(t *testing.T) {
for envKey, envVal := range ttp.env {
os.Setenv(envKey, envVal)
}

config, err := LoadConfig()
assert.Nil(t, err, "Unexpected error")

assert.Equal(t, ttp.wantConfig, config, "Unexpected config")

// unset envs for the next test
for envKey := range ttp.env {
os.Unsetenv(envKey)
}
})
}
}
83 changes: 0 additions & 83 deletions env.go

This file was deleted.

Loading
Loading