Waitlist script (#573)

* bash refactoring

* add storj.sh and user.sh

* reformat/refactor

* formatting

* fix typo
This commit is contained in:
Bryan White 2018-11-06 18:43:20 +01:00 committed by GitHub
parent deb015970d
commit 7257079c67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 236 additions and 61 deletions

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/usr/bin/env bash
source $(dirname $0)/utils.sh
basepath=$HOME/.storj/capt basepath=$HOME/.storj/capt
alpha_config=$basepath/config-alpha.yaml alpha_config=$basepath/config-alpha.yaml
unauthorized_config=$basepath/config-unauthorized.yaml unauthorized_config=$basepath/config-unauthorized.yaml
@ -7,69 +8,75 @@ ca_count=5
ca_basepath=$basepath/ca-alpha- ca_basepath=$basepath/ca-alpha-
ca_i_basepath() { ca_i_basepath() {
echo "$ca_basepath$1" echo ${ca_basepath}${1}
} }
rand_ca_basepath() { rand_ca_basepath() {
let i="($RANDOM % $ca_count) + 1" let i="($RANDOM % $ca_count) + 1"
echo $(ca_i_basepath $i) echo $(ca_i_basepath ${i})
} }
case $1 in case $1 in
--help) --help)
echo "usage: $(basename $0) [setup|alpha|unauthorized]" echo "usage: $(basename $0) [setup|alpha|unauthorized]"
;; ;;
setup) setup)
echo "setting up captplanet" temp_build captplanet identity
captplanet setup --overwrite echo "setting up captplanet"
echo "clearing whitelist" "$captplanet" setup --overwrite
echo > $ca_whitelist echo "clearing whitelist"
echo -n "generating alpha certificate authorities.." echo > ${ca_whitelist}
for i in $(seq 1 $ca_count); do echo -n "generating alpha certificate authorities.."
echo -n "$i.." for i in $(seq 1 ${ca_count}); do
_basepath=$(ca_i_basepath $i) echo -n "$i.."
identity ca new --ca.overwrite \ _basepath=$(ca_i_basepath ${i})
--ca.cert-path $_basepath.cert \ ${identity} ca new --ca.overwrite \
--ca.key-path $_basepath.key --ca.cert-path ${_basepath}.cert \
cat $_basepath.cert >> $ca_whitelist --ca.key-path ${_basepath}.key
done cat ${_basepath}.cert >> ${ca_whitelist}
echo "done" done
echo -n "generating alpha identities" echo "done"
for dir in $basepath/{f*,sat*,up*}; do echo -n "generating alpha identities"
echo -n "." for dir in ${basepath}/{f*,sat*,up*}; do
_ca_basepath=$(rand_ca_basepath) echo -n "."
_ca_cert=$dir/ca-alpha.cert _ca_basepath=$(rand_ca_basepath)
_ca_key=$dir/ca-alpha.key _ca_cert=${dir}/ca-alpha.cert
identity ca new --ca.overwrite \ _ca_key=${dir}/ca-alpha.key
--ca.cert-path $_ca_cert \ ${identity} ca new --ca.overwrite \
--ca.key-path $_ca_key \ --ca.cert-path ${_ca_cert} \
--ca.parent-cert-path $_ca_basepath.cert \ --ca.key-path ${_ca_key} \
--ca.parent-key-path $_ca_basepath.key --ca.parent-cert-path ${_ca_basepath}.cert \
identity id new --identity.overwrite \ --ca.parent-key-path ${_ca_basepath}.key
--identity.cert-path $dir/identity-alpha.cert \ ${identity} id new --identity.overwrite \
--identity.key-path $dir/identity-alpha.key \ --identity.cert-path ${dir}/identity-alpha.cert \
--ca.cert-path $_ca_cert \ --identity.key-path ${dir}/identity-alpha.key \
--ca.key-path $_ca_key --ca.cert-path ${_ca_cert} \
done --ca.key-path ${_ca_key}
echo "done" done
echo "writing alpha config" echo "done"
cat $basepath/config.yaml | \ echo "writing alpha config"
sed "s,peer-ca-whitelist-path: \"\",peer-ca-whitelist-path: $ca_whitelist,g" | \ cat ${basepath}/config.yaml | \
sed -E 's,cert-path: (.+)\.cert,cert-path: \1-alpha.cert,g' | \ sed "s,peer-ca-whitelist-path: \"\",peer-ca-whitelist-path: $ca_whitelist,g" | \
sed -E 's,key-path: (.+)\.key,key-path: \1-alpha.key,g' \ sed -E 's,cert-path: (.+)\.cert,cert-path: \1-alpha.cert,g' | \
> $alpha_config sed -E 's,key-path: (.+)\.key,key-path: \1-alpha.key,g' \
echo "writing unauthorized config" > ${alpha_config}
cat $basepath/config.yaml | sed -E "s,peer-ca-whitelist-path: \"\",peer-ca-whitelist-path: $ca_whitelist,g" > "$unauthorized_config" echo "writing unauthorized config"
;; cat ${basepath}/config.yaml | sed -E "s,peer-ca-whitelist-path: \"\",peer-ca-whitelist-path: $ca_whitelist,g" >"$unauthorized_config"
alpha) ;;
captplanet run --config $alpha_config alpha)
;; build captplanet
unauthorized) ${captplanet} run --config ${alpha_config}
captplanet run --config $unauthorized_config ;;
;; unauthorized)
run) build captplanet
captplanet run ${captplanet} run --config ${unauthorized_config}
;; ;;
*) run)
$0 --help ${captplanet} run
;; ;;
*)
$@ --help
;;
esac esac
rm -rf ${tmp_dir}

64
scripts/utils.sh Normal file
View File

@ -0,0 +1,64 @@
#!/usr/bin/env bash
dots() {
echo -n "."
sleep 1
dots
}
dots_on() {
dots &
dots_pid=$!
}
dots_off() {
disown $dots_pid
kill "$dots_pid"
}
build() {
local tmp_dir=$1
shift
echo "building temp binaries:"
for cmd in $@; do
echo -n " building $cmd..."
dots_on
local path=${tmp_dir}/${cmd}
declare -g ${cmd}=${path}
go build -o ${path} storj.io/storj/cmd/${cmd}
dots_off
echo "done"
done
echo " binaries built in $tmp_dir"
}
temp_build() {
tmp_dir=$(mktemp -d)
build ${tmp_dir} $@
}
check_help() {
if [ $1 == "--help" ] || [ $1 == "-h" ]; then
echo $2
exit 0
fi
}
ensure_dir() {
if [ ! -d $1 ]; then
mkdir $1
fi
}
no_overwrite() {
if [ -e $1 ]; then
echo "Error: $1 already exists; refusing to overwrite"
exit 10
fi
}
log_list() {
for f in $@; do
echo ${f}
done
}

46
scripts/waitlist/storj.sh Executable file
View File

@ -0,0 +1,46 @@
#!/usr/bin/env bash
source $(dirname $0)/../utils.sh
comment() {
cat << EOF
-----BEGIN COMMENT-----
Label: $1
Description: $2
-----END COMMENT-----
EOF
}
case $1 in
--help)
echo "usage: $0 new"
;;
new)
shift
check_help $1 "usage: storj.sh new <label> <output dir> [<whitelist path>]"
temp_build identity
label=$1
out_dir=$2
whitelist=$3
cert_path=${out_dir}/${label}.cert
key_path=${out_dir}/${label}.key
ensure_dir ${out_dir}
no_overwrite ${cert_path}
no_overwrite ${key_path}
${identity} ca new \
--ca.cert-path ${cert_path} \
--ca.key-path ${key_path}
echo "wrote:"
log_list ${cert_path} ${key_path}
if [ $# -gt 2 ]; then
comment ${label} >> ${whitelist}
cat ${cert_path} >> ${whitelist}
echo "appended to whitelist at $whitelist"
fi
;;
*)
$0 --help
;;
esac

58
scripts/waitlist/user.sh Executable file
View File

@ -0,0 +1,58 @@
#!/usr/bin/env bash
source $(dirname $0)/../utils.sh
new_ca() {
${identity} ca new \
--ca.cert-path ${ca_cert_path} \
--ca.key-path ${ca_key_path} \
--ca.parent-cert-path ${parent_cert_path} \
--ca.parent-key-path ${parent_key_path}
}
case $1 in
--help)
echo "usage: user.sh new|batch"
;;
new)
shift
check_help $1 "usage: identity.sh new <parent dir> <parent label> <label> <output dir>"
temp_build identity
parent_cert_path=${1}/${2}.cert
parent_key_path=${1}/${2}.key
ca_cert_path=${4}/${3}.cert
ca_key_path=${4}/${3}.key
ensure_dir $4
no_overwrite ${ca_cert_path}
no_overwrite ${ca_key_path}
new_ca
echo "wrote:"
log_list ${ca_cert_path} ${ca_key_path}
echo "certificate signed by cert:"
log_list ${parent_cert_path} ${parent_key_path}
;;
batch)
shift
check_help $1 "usage: user.sh batch <labels file> <parent dir> <parent label> <output dir>"
temp_build identity
labels=$(cat $1)
for label in ${labels}; do
parent_cert_path=${2}/${3}.cert
parent_key_path=${2}/${3}.key
ca_cert_path=${4}/${label}.cert
ca_key_path=${4}/${label}.key
ensure_dir $4
no_overwrite ${ca_cert_path}
no_overwrite ${ca_key_path}
new_ca
log_list ${ca_cert_path} ${ca_key_path}
done
echo "certificates signed by cert:"
log_list ${parent_cert_path} ${parent_key_path}
;;
esac