-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3000aa1
commit 6ec63de
Showing
10 changed files
with
228 additions
and
3 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/bin | ||
.DS_Store | ||
.test/ |
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,17 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/dev-drprasad/porter-hashicorp-plugins/pkg" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var runCmd = &cobra.Command{ | ||
Use: "run [implementation]", | ||
Short: "Run the plugin and listen for client connections.", | ||
Args: cobra.ExactValidArgs(1), | ||
ValidArgs: []string{pkg.VaultPluginInterface}, | ||
SilenceUsage: true, // Lets not send usage text to caller when error happens | ||
Run: func(cmd *cobra.Command, args []string) { | ||
p.Run(args) | ||
}, | ||
} |
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 |
---|---|---|
@@ -1,7 +1,13 @@ | ||
package main | ||
|
||
import "github.com/dev-drprasad/porter-hashicorp-plugins/cmd" | ||
import ( | ||
"os" | ||
|
||
"github.com/dev-drprasad/porter-hashicorp-plugins/cmd" | ||
) | ||
|
||
func main() { | ||
cmd.Execute() | ||
if err := cmd.Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
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,23 @@ | ||
package vault | ||
|
||
import ( | ||
"get.porter.sh/porter/pkg/secrets" | ||
cnabsecrets "github.com/cnabio/cnab-go/secrets" | ||
"github.com/dev-drprasad/porter-hashicorp-plugins/pkg/config" | ||
plugin "github.com/hashicorp/go-plugin" | ||
) | ||
|
||
var _ cnabsecrets.Store = &Plugin{} | ||
|
||
// Plugin is the plugin wrapper for accessing secrets from Vault. | ||
type Plugin struct { | ||
cnabsecrets.Store | ||
} | ||
|
||
func NewPlugin(cfg config.Config) plugin.Plugin { | ||
return &secrets.Plugin{ | ||
Impl: &Plugin{ | ||
Store: NewStore(cfg), | ||
}, | ||
} | ||
} |
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,74 @@ | ||
package vault | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"get.porter.sh/porter/pkg/secrets" | ||
cnabsecrets "github.com/cnabio/cnab-go/secrets" | ||
"github.com/dev-drprasad/porter-hashicorp-plugins/pkg/config" | ||
"github.com/hashicorp/vault/api" | ||
vaultapi "github.com/hashicorp/vault/api" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var _ cnabsecrets.Store = &Store{} | ||
|
||
const ( | ||
SecretKeyName = "secret" | ||
) | ||
|
||
// Store implements the backing store for secrets in azure key vault. | ||
type Store struct { | ||
config config.Config | ||
client *vaultapi.Client | ||
} | ||
|
||
func NewStore(cfg config.Config) cnabsecrets.Store { | ||
s := &Store{ | ||
config: cfg, | ||
} | ||
|
||
return secrets.NewSecretStore(s) | ||
} | ||
|
||
func (s *Store) Connect() error { | ||
if s.client != nil { | ||
return nil | ||
} | ||
|
||
config := &vaultapi.Config{ | ||
Address: s.config.VaultAddr, | ||
} | ||
client, err := api.NewClient(config) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not connect to vault server with address %s", s.config.VaultAddr) | ||
} | ||
s.client = client | ||
s.client.SetToken("s.BRGSWg7rIGxfq4ZsBQiCZ8Ki") | ||
|
||
return nil | ||
} | ||
|
||
func (s *Store) Resolve(keyName string, keyValue string) (string, error) { | ||
if strings.ToLower(keyName) != SecretKeyName { | ||
return "", errors.Errorf("cannot resolve unsupported keyName '%s'. Vault plugin only supports '%s' right now", keyName, SecretKeyName) | ||
} | ||
|
||
vaultSecret, err := s.client.Logical().Read(s.config.PathPrefix + "/data/" + keyValue) | ||
if err != nil || vaultSecret == nil { | ||
panic(err) | ||
} | ||
|
||
data, ok := vaultSecret.Data["data"] | ||
if !ok { | ||
return "", errors.New("property 'data' does not exist in secret") | ||
} | ||
|
||
secretB, err := json.Marshal(data) | ||
if err != nil { | ||
return "", errors.Wrap(err, "could not marshal secret data") | ||
} | ||
|
||
return string(secretB), nil | ||
} |