Repository navigation
Add Ansible vault plugin #382
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
Open
SimonBarendse
wants to merge
12
commits into
main
Choose a base branch
from
simon/ansible-vault-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4428ba8
feat: create ansible vault plugin
Sirz3chs 694cd6d
feat: add extra line to text file
Sirz3chs 472dba1
feat: throw an error if password field is not present
Sirz3chs 0ea287c
Refactor passwordFile function to simplify
SimonBarendse c304012
Fix lint error
SimonBarendse b93c98a
Sort imports
SimonBarendse 9f8af59
Expect password file for ansible-vault in /tmp/ directory
SimonBarendse 6c791bf
Add missing newline to ansible-vault password file
SimonBarendse 16e3a8c
Read ansible-vault password from homedir in test
SimonBarendse 04ff354
Fix: Remove trailing newline from imported ansible vault password
SimonBarendse 026f8d2
Merge branch 'main' into simon/ansible-vault-plugin
accraw dd1c621
merged in main
accraw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,25 @@ | ||
| package ansiblevault | ||
|
|
||
| import ( | ||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/needsauth" | ||
| "github.com/1Password/shell-plugins/sdk/schema" | ||
| "github.com/1Password/shell-plugins/sdk/schema/credname" | ||
| ) | ||
|
|
||
| func AnsibleVaultCLI() schema.Executable { | ||
| return schema.Executable{ | ||
| Name: "Ansible Vault CLI", | ||
| Runs: []string{"ansible-vault"}, | ||
| DocsURL: sdk.URL("https://docs.ansible.com/ansible/latest/cli/ansible-vault.html"), | ||
| NeedsAuth: needsauth.IfAll( | ||
| needsauth.NotForHelpOrVersion(), | ||
| needsauth.NotWithoutArgs(), | ||
| ), | ||
| Uses: []schema.CredentialUsage{ | ||
| { | ||
| Name: credname.Credential, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or 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,63 @@ | ||
| package ansiblevault | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/importer" | ||
| "github.com/1Password/shell-plugins/sdk/provision" | ||
| "github.com/1Password/shell-plugins/sdk/schema" | ||
| "github.com/1Password/shell-plugins/sdk/schema/credname" | ||
| "github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
| ) | ||
|
|
||
| func Password() schema.CredentialType { | ||
| return schema.CredentialType{ | ||
| Name: credname.Credential, | ||
| DocsURL: sdk.URL("https://docs.ansible.com/ansible/latest/cli/ansible-vault.html"), | ||
| ManagementURL: nil, | ||
| Fields: []schema.CredentialField{ | ||
| { | ||
| Name: fieldname.Password, | ||
| MarkdownDescription: "Password to use when encrypting or decrypting.", | ||
| Secret: true, | ||
| }, | ||
| }, | ||
| DefaultProvisioner: provision.TempFile( | ||
| passwordFile, | ||
| provision.Filename(".ansible-vault"), | ||
| provision.SetPathAsEnvVar("ANSIBLE_VAULT_PASSWORD_FILE"), | ||
| ), | ||
| Importer: importer.TryAll( | ||
| TryPasswordFile(), | ||
| )} | ||
| } | ||
|
|
||
| func passwordFile(in sdk.ProvisionInput) ([]byte, error) { | ||
| if password, ok := in.ItemFields[fieldname.Password]; ok { | ||
| return []byte(password + "\n"), nil | ||
| } | ||
| return nil, fmt.Errorf("unable to find password field") | ||
| } | ||
|
|
||
| func TryPasswordFile() sdk.Importer { | ||
| file := os.Getenv("ANSIBLE_VAULT_PASSWORD_FILE") | ||
| if file == "" { | ||
| file = "~/.ansible-vault" | ||
|
SimonBarendse marked this conversation as resolved.
|
||
| } | ||
| return importer.TryFile(file, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { | ||
| password := strings.TrimSuffix(contents.ToString(), "\n") | ||
| if password == "" { | ||
| return | ||
| } | ||
|
|
||
| out.AddCandidate(sdk.ImportCandidate{ | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Password: password, | ||
| }, | ||
| }) | ||
| }) | ||
| } | ||
This file contains hidden or 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,46 @@ | ||
| package ansiblevault | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/plugintest" | ||
| "github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
| ) | ||
|
|
||
| func TestPasswordProvisioner(t *testing.T) { | ||
| plugintest.TestProvisioner(t, Password().DefaultProvisioner, map[string]plugintest.ProvisionCase{ | ||
| "default": { | ||
| ItemFields: map[sdk.FieldName]string{ | ||
| fieldname.Password: "Sl9WhfECfUn43o2qEcN2GnAw2EXAMPLE", | ||
| }, | ||
| ExpectedOutput: sdk.ProvisionOutput{ | ||
| Files: map[string]sdk.OutputFile{ | ||
| "/tmp/.ansible-vault": { | ||
| Contents: []byte(plugintest.LoadFixture(t, ".ansible-vault")), | ||
| }, | ||
| }, | ||
| Environment: map[string]string{ | ||
| "ANSIBLE_VAULT_PASSWORD_FILE": "/tmp/.ansible-vault", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestPasswordImporter(t *testing.T) { | ||
| plugintest.TestImporter(t, Password().Importer, map[string]plugintest.ImportCase{ | ||
| "default": { | ||
| Files: map[string]string{ | ||
| "~/.ansible-vault": plugintest.LoadFixture(t, ".ansible-vault"), | ||
| }, | ||
| ExpectedCandidates: []sdk.ImportCandidate{ | ||
| { | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Password: "Sl9WhfECfUn43o2qEcN2GnAw2EXAMPLE", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } |
This file contains hidden or 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,22 @@ | ||
| package ansiblevault | ||
|
|
||
| import ( | ||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/schema" | ||
| ) | ||
|
|
||
| func New() schema.Plugin { | ||
| return schema.Plugin{ | ||
| Name: "ansiblevault", | ||
| Platform: schema.PlatformInfo{ | ||
| Name: "Ansible Vault", | ||
| Homepage: sdk.URL("https://docs.ansible.com/ansible/latest/cli/ansible-vault.html"), | ||
| }, | ||
| Credentials: []schema.CredentialType{ | ||
| Password(), | ||
| }, | ||
| Executables: []schema.Executable{ | ||
| AnsibleVaultCLI(), | ||
| }, | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| Sl9WhfECfUn43o2qEcN2GnAw2EXAMPLE |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd call the plugin
ansible. Theansible-vaultCLI is shipped as part of theansiblepackage.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't that be confusing if we add the ansible CLI as well? Or are you just suggesting we rename the package? It looks like there are 9 CLIs: https://docs.ansible.com/ansible/latest/command_guide/command_line_tools.html