-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from MeilleursAgents/feat_ansiblevault_string
Feat ansiblevault string
- Loading branch information
Showing
9 changed files
with
268 additions
and
15 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
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
File renamed without changes.
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,52 @@ | ||
package provider | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/MeilleursAgents/terraform-provider-ansiblevault/pkg/vault" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func inStringResource() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: inStringRead, | ||
Schema: map[string]*schema.Schema{ | ||
"encrypted": { | ||
Type: schema.TypeString, | ||
Description: "Ansible-vault string representation", | ||
Required: true, | ||
}, | ||
"key": { | ||
Type: schema.TypeString, | ||
Description: "Vault key searched", | ||
Required: true, | ||
}, | ||
"value": { | ||
Computed: true, | ||
Description: "Vault value found", | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func inStringRead(data *schema.ResourceData, m interface{}) error { | ||
raw := data.Get("encrypted").(string) | ||
key := data.Get("key").(string) | ||
|
||
data.SetId(time.Now().UTC().String()) | ||
|
||
value, err := m.(*vault.App).InString(raw, key) | ||
|
||
if err != nil { | ||
data.SetId("") | ||
return err | ||
} | ||
|
||
if err := data.Set("value", value); err != nil { | ||
data.SetId("") | ||
return err | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package provider | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
|
||
"github.com/MeilleursAgents/terraform-provider-ansiblevault/pkg/vault" | ||
) | ||
|
||
func TestInStringRead(t *testing.T) { | ||
vaultRaw := `$ANSIBLE_VAULT;1.1;AES256 | ||
61336365316161396566653134393964613564646439313333666233356463336131336537303633 | ||
6239626439383636346130653132326138313437306365310a663961653131373535633431393836 | ||
34353035376531643266383736306338333764373837656131323663396435666332343039666465 | ||
3635613231313833650a346365623861663638313830616564623663386137303735356639313163 | ||
34343639636161656230363030353763623830653838333166623234326334663338` | ||
|
||
var cases = []struct { | ||
intention string | ||
input string | ||
key string | ||
want string | ||
wantErr error | ||
}{ | ||
{ | ||
"simple", | ||
vaultRaw, | ||
"API_KEY", | ||
"NOT_IN_CLEAR_TEXT", | ||
nil, | ||
}, | ||
{ | ||
"not found key", | ||
vaultRaw, | ||
"SECRET_KEY", | ||
"", | ||
vault.ErrKeyNotFound, | ||
}, | ||
} | ||
|
||
for _, testCase := range cases { | ||
t.Run(testCase.intention, func(t *testing.T) { | ||
data := inStringResource().Data(nil) | ||
|
||
if err := data.Set("encrypted", testCase.input); err != nil { | ||
t.Errorf("unable to set encrypted: %s", err) | ||
return | ||
} | ||
|
||
data.Set("key", testCase.key) | ||
if err := data.Set("key", testCase.key); err != nil { | ||
t.Errorf("unable to set key: %s", err) | ||
return | ||
} | ||
|
||
vaultApp, err := vault.New(path.Join(ansibleFolder, "vault_pass_test.txt"), ansibleFolder, "") | ||
if err != nil { | ||
t.Errorf("unable to create vault app: %#v", err) | ||
return | ||
} | ||
|
||
err = inStringRead(data, vaultApp) | ||
result := data.Get("value").(string) | ||
|
||
failed := false | ||
|
||
if err == nil && testCase.wantErr != nil { | ||
failed = true | ||
} else if err != nil && testCase.wantErr == nil { | ||
failed = true | ||
} else if err != nil && err.Error() != testCase.wantErr.Error() { | ||
failed = true | ||
} else if result != testCase.want { | ||
failed = true | ||
} | ||
|
||
if failed { | ||
t.Errorf("inStringRead() = (`%s`, %#v), want (`%s`, %#v)", result, err, testCase.want, testCase.wantErr) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.