This repository has been archived by the owner on Jul 19, 2019. It is now read-only.
forked from Varying-Vagrant-Vagrants/VVV
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitbucket-gen-key.sh
executable file
·176 lines (141 loc) · 3.78 KB
/
bitbucket-gen-key.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
# via https://github.com/rcelha/vagrant-sh-provisioner-scripts/
gen_key(){
local ID_FILE=$1;
if [ "${ID_FILE}" == "" ]; then
echo ID_FILE not defined;
return 1;
fi;
if [ -f "${ID_FILE}" ]; then
echo "The file ${ID_FILE} already exists.";
echo "Recording SSH config..."
record_ssh_config bitbucket.org $ID_FILE ~/.ssh/config;
echo "Copying key to config/ssh..."
CURRENT_DIR=`dirname $0`
mkdir -p $CURRENT_DIR/config/ssh
cp ~/.ssh/bitbucket.org_id_rsa* $CURRENT_DIR/config/ssh/
return 1;
fi;
ssh-keygen -f ${ID_FILE};
return $?;
}
retrieve_user(){
local SERVICE_NAME=$1;
local SERVICE_USERNAME;
read -p "$SERVICE_NAME user: " SERVICE_USERNAME;
echo $SERVICE_USERNAME;
return 0;
}
retrieve_password(){
local SERVICE_NAME=$1;
local SERVICE_PASSWORD;
read -s -p "password for ${SERVICE_NAME}: " SERVICE_PASSWORD;
echo $SERVICE_PASSWORD;
return 0;
}
_send_key(){
echo "Not implemented";
exit 1;
}
send_key(){
local RET;
local CURL_RET;
local RETCODE;
RET=`_send_key ${@}`;
CURL_RET=$?;
if [ $CURL_RET -ne 0 ]; then
echo ;
echo "Erro on curl command";
echo $RET;
return $CURL_RET;
fi;
RETCODE=`echo "${RET}" | grep RETCODE | cut -d : -f2`;
echo $RETCODE;
if [ $RETCODE -ge 400 ]; then
echo;
echo Erro while send the ssh key;
echo "[API RESPONSE]";
echo "${RET}"
return 1;
fi;
}
record_ssh_config(){
local SERVICE_NAME=$1;
local ID_FILE=$2;
local SSH_CONFIG=$3;
touch "$SSH_CONFIG"
CONFIG_IS_SET=`cat ~/.ssh/config | grep "Host $SERVICE_NAME"`;
if [ ! -z "$CONFIG_IS_SET" ]; then
echo "~/.ssh/config is already configured"
return 0;
else
echo "Configuring ~/.ssh/config"
echo "
# $SERVICE_NAME CONFIG
Host $SERVICE_NAME
HostName $SERVICE_NAME
PreferredAuthentications publickey
StrictHostKeyChecking no
IdentityFile ${ID_FILE}" >> ${SSH_CONFIG};
chmod 600 ${SSH_CONFIG};
fi
CURRENT_DIR=`dirname $0`
touch $CURRENT_DIR/config/ssh/config
CONFIG_IS_SET=`cat config/ssh/config | grep "Host $SERVICE_NAME"`;
if [ ! -z "$CONFIG_IS_SET" ]; then
echo "config/ssh/config is already configured"
return 0;
else
echo "Configuring config/ssh/config"
mkdir -p $CURRENT_DIR/config/ssh
echo "
# $SERVICE_NAME CONFIG
Host $SERVICE_NAME
HostName $SERVICE_NAME
PreferredAuthentications publickey
StrictHostKeyChecking no
IdentityFile /home/vagrant/.ssh/${SERVICE_NAME}_id_rsa" >> $CURRENT_DIR/config/ssh/config;
fi
return 0;
}
gen_key_main(){
local SERVICE_NAME=$1;
local ID_FILE=${HOME}/.ssh/${SERVICE_NAME}_id_rsa;
local ID_FILE_PUB=${ID_FILE}.pub;
local SSH_CONFIG=${HOME}/.ssh/config;
local USERNAME;
local PASSWORD;
local KEY_VALUE;
local CURL_COMMAND;
gen_key $ID_FILE $SSH_CONFIG;
if [ $? != 0 ]; then
exit 1;
fi;
USERNAME=`retrieve_user ${SERVICE_NAME}`;
PASSWORD=`retrieve_password ${SERVICE_NAME}`;
send_key $ID_FILE_PUB $USERNAME $PASSWORD;
if [ $? != 0 ]; then
exit 1;
fi;
record_ssh_config $SERVICE_NAME $ID_FILE $SSH_CONFIG;
CURRENT_DIR=`dirname $0`
mkdir -p $CURRENT_DIR/config/ssh
cp ~/.ssh/bitbucket.org_id_rsa* $CURRENT_DIR/config/ssh/
echo ;
echo OK;
echo ;
return 0;
}
_send_key(){
local ID_FILE_PUB=$1;
local USERNAME=$2;
local PASSWORD=$3;
local CURL;
local KEY_VALUE;
KEY_VALUE=`cat ${ID_FILE_PUB}`;
CURL="curl -k -X POST -sL -w \nRETCODE:%{http_code} ";
CURL="${CURL} --user ${USERNAME}:${PASSWORD}";
$CURL https://api.bitbucket.org/1.0/users/${USERNAME}/ssh-keys/ -F "key=${KEY_VALUE}" -F "label=vagrant";
return $?;
}
gen_key_main bitbucket.org;