-
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.
feat: support HashiCorp Vault with token based access (#38)
* feat: add source system support for hashicorp vault * feat: implement cli flag for pointing to specific config file * fix: wrong type in filesystem config
- Loading branch information
1 parent
d078e6d
commit 8e932bc
Showing
20 changed files
with
536 additions
and
19 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 |
---|---|---|
|
@@ -13,3 +13,6 @@ trim_trailing_whitespace = true | |
|
||
[{*.go,Makefile,go.mod,go.sum}] | ||
indent_style = tab | ||
|
||
[*.sh] | ||
indent_size = 4 |
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 |
---|---|---|
@@ -1,11 +1,21 @@ | ||
# In order to enable environmental substitution, it is necessary to include all potential configurations in this | ||
# configuration file. Otherwise, the environment variables will not be automatically added to the ResourceConfig | ||
# (`mapstructure:",remain"`). | ||
|
||
loglevel: info | ||
|
||
source: | ||
type: filesystem | ||
|
||
filesystem: | ||
path: /tmp/source.txt | ||
|
||
hashicorpvault: | ||
token: | ||
address: | ||
|
||
destination: | ||
type: filesystem | ||
|
||
filesystem: | ||
path: /tmp/destination.txt | ||
|
||
logLevel: debug |
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
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,70 @@ | ||
/* | ||
* Copyright 2023 cluetec GmbH | ||
* | ||
* 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 hashicorpvault | ||
|
||
import ( | ||
globalConfig "github.com/cluetec/lifeboat/internal/config" | ||
"github.com/go-playground/validator/v10" | ||
vault "github.com/hashicorp/vault/api" | ||
"github.com/mitchellh/mapstructure" | ||
"log/slog" | ||
) | ||
|
||
const Type = "hashicorpvault" | ||
|
||
type metaConfig struct { | ||
hashicorpvault config | ||
} | ||
|
||
type config struct { | ||
Address string `validate:"http_url,required"` | ||
Token string `validate:"required"` | ||
} | ||
|
||
var validate *validator.Validate | ||
|
||
// newConfig provides the specific `config` struct. It takes the generic `globalConfig.ResourceConfig` and | ||
// decodes it into the `config` struct and validates the values. | ||
func newConfig(rc *globalConfig.ResourceConfig) (*config, error) { | ||
var c metaConfig | ||
|
||
err := mapstructure.Decode(rc, &c) | ||
if err != nil { | ||
slog.Error("unable to decode config into HashiCorp Vault source config", "error", err) | ||
return nil, err | ||
} | ||
|
||
validate = validator.New() | ||
if err := validate.Struct(c); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &c.hashicorpvault, nil | ||
} | ||
|
||
// LogValue customizes how the `config` struct will be printed in the logs. | ||
func (c *config) LogValue() slog.Value { | ||
return slog.GroupValue(slog.String("address", c.Address), slog.String("token", "***")) | ||
} | ||
|
||
func (c *config) GetHashiCorpVaultConfig() *vault.Config { | ||
config := vault.Config{ | ||
Address: c.Address, | ||
} | ||
|
||
return &config | ||
} |
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,75 @@ | ||
/* | ||
* Copyright 2023 cluetec GmbH | ||
* | ||
* 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 hashicorpvault | ||
|
||
import ( | ||
globalConfig "github.com/cluetec/lifeboat/internal/config" | ||
vault "github.com/hashicorp/vault/api" | ||
"io" | ||
"log/slog" | ||
) | ||
|
||
const snapshotPath = "/sys/storage/raft/snapshot" | ||
|
||
// Reader implements the `io.ReaderClose` interface in order to read the backup from HashiCorp Vault. | ||
type Reader struct { | ||
client *vault.Client | ||
reader io.Reader | ||
} | ||
|
||
// NewReader initializes a new `Reader` struct which is implementing the `io.ReaderClose` interface. | ||
func NewReader(rc *globalConfig.ResourceConfig) (*Reader, error) { | ||
c, err := newConfig(rc) | ||
if err != nil { | ||
slog.Error("error while initializing source config", "sourceType", Type, "error", err) | ||
return nil, err | ||
} | ||
|
||
slog.Debug("source config loaded", "sourceType", Type, "config", c) | ||
|
||
client, err := vault.NewClient(c.GetHashiCorpVaultConfig()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client.SetToken(c.Token) | ||
return &Reader{client: client}, nil | ||
} | ||
|
||
func (r *Reader) Read(b []byte) (int, error) { | ||
slog.Debug("read got called", "sourceType", Type) | ||
|
||
if r.reader == nil { | ||
resp, err := r.client.Logical().ReadRaw(snapshotPath) | ||
if err != nil { | ||
slog.Error("failed to called backup endpoint", "error", err) | ||
return 0, err | ||
} | ||
|
||
r.reader = resp.Body | ||
} | ||
|
||
return r.reader.Read(b) | ||
} | ||
|
||
func (r *Reader) Close() error { | ||
slog.Debug("closing reader", "sourceType", Type) | ||
if closer, ok := r.reader.(io.Closer); ok { | ||
return closer.Close() | ||
} | ||
return 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
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,4 @@ | ||
.data/ | ||
backup-destination/*.snap | ||
vault-token.txt | ||
vault-unseal-keys.txt |
Oops, something went wrong.