Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions plugins/ansiblevault/ansible_vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ansiblevault

Copy link
Copy Markdown
Contributor

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. The ansible-vault CLI is shipped as part of the ansible package.

Copy link
Copy Markdown
Contributor

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


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,
},
},
}
}
63 changes: 63 additions & 0 deletions plugins/ansiblevault/password.go
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"
Comment thread
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,
},
})
})
}
46 changes: 46 additions & 0 deletions plugins/ansiblevault/password_test.go
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",
},
},
},
},
})
}
22 changes: 22 additions & 0 deletions plugins/ansiblevault/plugin.go
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(),
},
}
}
1 change: 1 addition & 0 deletions plugins/ansiblevault/test-fixtures/.ansible-vault
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sl9WhfECfUn43o2qEcN2GnAw2EXAMPLE
Loading