Skip to content

Commit

Permalink
Add process_env provider (#129)
Browse files Browse the repository at this point in the history
* Initial commit

* Added nolint for strings.SplitN

* Update pkg/providers/process_env.go

Co-authored-by: Elad Kaplan <kaplan.elad@gmail.com>

* Update pkg/providers/process_env.go

Co-authored-by: Elad Kaplan <kaplan.elad@gmail.com>

* Update pkg/providers/process_env.go

Co-authored-by: Elad Kaplan <kaplan.elad@gmail.com>

* Update pkg/providers/process_env.go

Co-authored-by: Elad Kaplan <kaplan.elad@gmail.com>

* Updates based on feedback

* Minor tweaks for return errors

* Updated template

Co-authored-by: Elad Kaplan <kaplan.elad@gmail.com>
  • Loading branch information
kevbook and kaplanelad committed Sep 4, 2022
1 parent 604abec commit 97ef49b
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,36 @@ providers:
path: /folder/bar
```


## ProcessEnv

Load the environment variables from the parent process as needed.

### Authentication
No need.

### Features

* Sync - `yes`
* Mapping - `yes`
* Modes - `read`
* Key format
* `env_sync` - Will return all the environment variables in the parent process
* `path` - Value isn't required or used
* `env`
* `field` - Optional field: specific environment variable in the parent process
### Example Config
```yaml
providers:
process_env:
env:
ETC_DSN:
# Optional: accesses the environment variable `SOME_KEY` and maps it to ETC_DSN
field: SOME_KEY
```
## Azure
### Authentication
Expand Down
3 changes: 3 additions & 0 deletions pkg/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (p *BuiltinProviders) ProviderHumanToMachine() map[string]string {
"GitHub": "github",
"KeyPass": "keypass",
"FileSystem": "filesystem",
"ProcessEnv": "process_env",
}
}

Expand Down Expand Up @@ -84,6 +85,8 @@ func (p *BuiltinProviders) GetProvider(name string) (core.Provider, error) { //n
return providers.NewKeyPass(logger)
case "filesystem":
return providers.NewFileSystem(logger)
case "process_env":
return providers.NewProcessEnv(logger)
default:
return nil, fmt.Errorf("provider '%s' does not exist", name)
}
Expand Down
81 changes: 81 additions & 0 deletions pkg/providers/process_env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package providers

import (
"fmt"
"os"
"sort"
"strings"

"github.com/spectralops/teller/pkg/core"

"github.com/spectralops/teller/pkg/logging"
)

type ProcessEnv struct {
logger logging.Logger
}

// NewProcessEnv creates new provider instance
func NewProcessEnv(logger logging.Logger) (core.Provider, error) {
return &ProcessEnv{
logger: logger,
}, nil
}

// Name return the provider name
func (a *ProcessEnv) Name() string {
return "process_env"
}

// GetMapping returns a multiple entries
func (a *ProcessEnv) GetMapping(p core.KeyPath) ([]core.EnvEntry, error) {
a.logger.Debug("read secret")

kvs := make(map[string]string)
for _, envs := range os.Environ() {
pair := strings.SplitN(envs, "=", 2) // nolint: gomnd
kvs[pair[0]] = pair[1]
}
var entries []core.EnvEntry
for k, v := range kvs {
entries = append(entries, p.FoundWithKey(k, v))
}
sort.Sort(core.EntriesByKey(entries))
return entries, nil
}

// Get returns a single entry
func (a *ProcessEnv) Get(p core.KeyPath) (*core.EnvEntry, error) {
a.logger.Debug("read secret")

k := p.EffectiveKey()
val, ok := os.LookupEnv(k)
if !ok {
a.logger.WithFields(map[string]interface{}{"key": k}).Debug("key not found")
ent := p.Missing()
return &ent, nil
}

ent := p.Found(val)
return &ent, nil
}

// Delete will delete entry
func (a *ProcessEnv) Delete(kp core.KeyPath) error {
return fmt.Errorf("provider %s does not implement delete yet", a.Name())
}

// DeleteMapping will delete the given path recessively
func (a *ProcessEnv) DeleteMapping(kp core.KeyPath) error {
return fmt.Errorf("provider %s does not implement deleteMapping yet", a.Name())
}

// Put will create a new single entry
func (a *ProcessEnv) Put(p core.KeyPath, val string) error {
return fmt.Errorf("provider %s does not implement put yet", a.Name())
}

// PutMapping will create a multiple entries
func (a *ProcessEnv) PutMapping(p core.KeyPath, m map[string]string) error {
return fmt.Errorf("provider %s does not implement putMapping yet", a.Name())
}
1 change: 1 addition & 0 deletions pkg/providers/process_env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package providers
9 changes: 9 additions & 0 deletions pkg/wizard_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,14 @@ providers:
ETC_DSN:
path: redis/config/foobar
{{end}}
{{- if index .ProviderKeys "process_env" }}
process_env:
env:
ETC_DSN:
field: SOME_KEY # Optional, accesses the environment variable SOME_KEY and maps it to ETC_DSN
{{end}}
`

0 comments on commit 97ef49b

Please sign in to comment.