0
0
forked from toolshed/abra

Compare commits

..

1 Commits

Author SHA1 Message Date
bd80599114 secret: allow inserting secret from file and add trim flag 2024-06-22 17:28:13 +02:00
5 changed files with 64 additions and 34 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt"
"os"
"strconv"
"strings"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/autocomplete"
@ -156,6 +157,8 @@ var appSecretInsertCommand = cli.Command{
Flags: []cli.Flag{
internal.DebugFlag,
internal.PassFlag,
internal.FileFlag,
internal.TrimFlag,
},
Before: internal.SubCommandBefore,
ArgsUsage: "<domain> <secret-name> <version> <data>",
@ -188,6 +191,18 @@ Example:
version := c.Args().Get(2)
data := c.Args().Get(3)
if internal.File {
raw, err := os.ReadFile(data)
if err != nil {
logrus.Fatalf("reading secret from file: %s", err)
}
data = string(raw)
}
if internal.Trim {
data = strings.TrimSpace(data)
}
secretName := fmt.Sprintf("%s_%s_%s", app.StackName(), name, version)
if err := client.StoreSecret(cl, secretName, data, app.Server); err != nil {
logrus.Fatal(err)

View File

@ -38,6 +38,20 @@ var PassRemoveFlag = &cli.BoolFlag{
Destination: &PassRemove,
}
var File bool
var FileFlag = &cli.BoolFlag{
Name: "file, f",
Usage: "Treat input as a file",
Destination: &File,
}
var Trim bool
var TrimFlag = &cli.BoolFlag{
Name: "trim, t",
Usage: "Trim input",
Destination: &Trim,
}
// Force force functionality without asking.
var Force bool

View File

@ -50,7 +50,9 @@ teardown(){
assert_success
assert_exists "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
assert_equal $(_get_tag_hash 0.1.1+1.20.2) $(_get_current_hash)
_get_tag_hash 0.1.1+1.20.2
_get_current_hash
assert_equal "$tagHash" "$currentHash"
}
@test "does not overwrite existing env files" {
@ -111,12 +113,13 @@ teardown(){
@test "ensure recipe up to date if no --offline" {
_reset_recipe
wantHash=$(_get_n_hash 3)
_get_n_hash 3
run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3
assert_success
assert_equal $(_get_current_hash) "$wantHash"
_get_current_hash
assert_equal "$currentHash" "$nHash"
run $ABRA app new "$TEST_RECIPE" \
--no-input \
@ -125,19 +128,22 @@ teardown(){
assert_success
assert_exists "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
assert_equal $(_get_head_hash) $(_get_current_hash)
_get_head_hash
_get_current_hash
assert_equal "$HEAD_HASH" "$CURRENT_HASH"
_reset_recipe
}
@test "ensure recipe not up to date if --offline" {
_reset_recipe
wantHash=$(_get_n_hash 3)
_get_n_hash 3
run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3
assert_success
assert_equal $(_get_current_hash) "$wantHash"
_get_current_hash
assert_equal "$currentHash" "$nHash"
# NOTE(d1): need to use --chaos to force same commit
run $ABRA app new "$TEST_RECIPE" \
@ -149,7 +155,8 @@ teardown(){
assert_success
assert_exists "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
assert_equal $(_get_current_hash) "$wantHash"
_get_current_hash
assert_equal "$currentHash" "$nHash"
_reset_recipe
}

View File

@ -19,6 +19,13 @@ teardown_file(){
_reset_recipe
}
teardown() {
run $ABRA app secret rm "$TEST_APP_DOMAIN" --all
_reset_app
_reset_recipe
_checkout_recipe
}
setup(){
load "$PWD/tests/integration/helpers/common"
_common_setup
@ -77,9 +84,6 @@ setup(){
assert_output --partial 'test_pass_one'
assert_output --partial 'test_pass_two'
refute_output --partial 'extra_pass'
run $ABRA app secret rm "$TEST_APP_DOMAIN" --all
assert_success
}
@test "generate: broken if missing version" {
@ -91,7 +95,6 @@ setup(){
assert_failure
assert_output --partial 'missing version'
_reset_app
}
@test "generate: use version from app env" {
@ -108,11 +111,6 @@ setup(){
assert_success
assert_output --partial 'v2'
refute_output --partial 'v1'
run $ABRA app secret rm "$TEST_APP_DOMAIN" --all
assert_success
_reset_app
}
@test "generate: generate extra secret based on COMPOSE_FILE" {
@ -131,11 +129,6 @@ setup(){
run docker -c "$TEST_SERVER" secret ls
assert_success
assert_output --partial "$TEST_APP_DOMAIN_extra_pass_v1"
run $ABRA app secret rm "$TEST_APP_DOMAIN" --all
assert_success
_reset_app
}
@test "generate: bail if unstaged changes and no --chaos" {
@ -162,8 +155,6 @@ setup(){
run $ABRA app secret rm "$TEST_APP_DOMAIN" --all --chaos
assert_success
_checkout_recipe
}
@test "generate: ensure secret name uses trimmed stack name" {
@ -228,9 +219,22 @@ setup(){
run $ABRA app secret ls "$TEST_APP_DOMAIN"
assert_success
assert_output --partial 'true'
}
run $ABRA app secret rm "$TEST_APP_DOMAIN" test_pass_one
@test "insert: create secret from file" {
run $ABRA app secret ls "$TEST_APP_DOMAIN"
assert_success
assert_output --partial 'false'
run bash -c "echo bar >> $ABRA_DIR/recipes/$TEST_RECIPE/foo"
run $ABRA app secret insert --file "$TEST_APP_DOMAIN" test_pass_one v1 "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
assert_success
assert_output --partial 'successfully stored on server'
run $ABRA app secret ls "$TEST_APP_DOMAIN"
assert_success
assert_output --partial 'true'
}
@test "rm: validate arguments" {
@ -314,9 +318,6 @@ setup(){
run $ABRA app secret ls "$TEST_APP_DOMAIN"
assert_success
assert_output --partial 'true'
run $ABRA app secret rm "$TEST_APP_DOMAIN" --all
assert_success
}
@test "ls: show secrets as machine readable" {
@ -330,9 +331,6 @@ setup(){
run $ABRA app secret ls "$TEST_APP_DOMAIN" --machine
assert_success
assert_output --partial '"created-on-server":"true"'
run $ABRA app secret rm "$TEST_APP_DOMAIN" --all
assert_success
}
@test "ls: bail if unstaged changes and no --chaos" {

View File

@ -48,23 +48,19 @@ _git_commit() {
_get_tag_hash() {
tagHash=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" rev-list -n 1 "$1")
assert_success
echo "$tagHash"
}
_get_head_hash() {
headHash=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" show -s --format="%H" HEAD)
assert_success
echo "$headHash"
}
_get_current_hash() {
currentHash=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" show -s --format="%H")
assert_success
echo "$currentHash"
}
_get_n_hash() {
nHash=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" show -s --format="%H" "HEAD~$1")
assert_success
echo "$nHash"
}