1a727d3ff8a1668898cd0326902a9378746fab30
[it/dep.git] / ric-infra / 10-Nexus / bin / change_password
1 #!/bin/bash
2 ################################################################################
3 #   Copyright (c) 2019 AT&T Intellectual Property.                             #
4 #   Copyright (c) 2019 Nokia.                                                  #
5 #                                                                              #
6 #   Licensed under the Apache License, Version 2.0 (the "License");            #
7 #   you may not use this file except in compliance with the License.           #
8 #   You may obtain a copy of the License at                                    #
9 #                                                                              #
10 #       http://www.apache.org/licenses/LICENSE-2.0                             #
11 #                                                                              #
12 #   Unless required by applicable law or agreed to in writing, software        #
13 #   distributed under the License is distributed on an "AS IS" BASIS,          #
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15 #   See the License for the specific language governing permissions and        #
16 #   limitations under the License.                                             #
17 ################################################################################
18
19 OVERRIDEYAML=$1
20
21
22 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
23 source $DIR/../etc/nexus.conf
24
25 if [ -z "$RICINFRA_RELEASE_NAME" ];then
26    RELEASE_NAME=$helm_release_name
27 else
28    RELEASE_NAME=$RICINFRA_RELEASE_NAME
29 fi
30 if [ -z "$RICINFRA_NAMESPACE" ];then
31    NAMESPACE=$namespace
32 else
33    NAMESPACE=$RICINFRA_NAMESPACE
34 fi
35 if [ -z "$INGRESS_PORT" ];then
36    INGRESS_PORT_NEXUS=$ingress_port
37 else
38    INGRESS_PORT_NEXUS=$INGRESS_PORT
39 fi
40
41 if [ -z "$DEFAULT_NEXUS_ADMIN_PASSWORD" ];then
42    NEXUS_ADMIN_PASSWORD=$default_admin_password
43 else
44    NEXUS_ADMIN_PASSWORD=$DEFAULT_NEXUS_ADMIN_PASSWORD
45 fi
46
47
48
49 HOSTPOSTFIX=$(cat $DIR/../helm/values.yaml  | grep hostpostfix | awk '{print $2}')
50
51
52
53
54
55
56
57
58
59 NEXUS_URL="http://nexus.$HOSTPOSTFIX:$INGRESS_PORT_NEXUS"
60
61
62
63
64
65
66 if [ -z $OVERRIDEYAML ]; then
67
68   DOCKERPASSWORD=$(cat $DIR/../../20-Credential/helm/values.yaml | awk '/^.*repositoryCredential:.*/{getline; getline;  print substr($2, 2, length($2)-2);}')
69   HELMPASSWORD=$(cat $DIR/../../20-Credential/helm/values.yaml | awk '/^.*helmrepoCredential:.*/{getline; getline;  print substr($2, 2, length($2)-2);}')
70   ADMINPASSWORD=$(cat $DIR/../helm/values.yaml | awk '/^.*adminPassword:.*/{print $2}')
71
72 else
73
74
75
76   DOCKERPASSWORD=$(cat $OVERRIDEYAML | awk '/^.*repositoryCredential:.*/{getline; getline;  print substr($2, 2, length($2)-2);}')
77
78
79
80   if [ -z $DOCKERPASSWORD ]; then
81     DOCKERPASSWORD=$(cat $DIR/../../20-Credential/helm/values.yaml | awk '/^.*repositoryCredential:.*/{getline; getline;  print substr($2, 2, length($2)-2);}')
82   fi
83
84   HELMPASSWORD=$(cat $OVERRIDEYAML | awk '/^.*helmrepoCredential:.*/{getline; getline;  print substr($2, 2, length($2)-2);}')
85  
86   if [ -z $HELMPASSWORD ]; then
87     HELMPASSWORD=$(cat $DIR/../../20-Credential/helm/values.yaml | awk '/^.*helmrepoCredential:.*/{getline; getline;  print substr($2, 2, length($2)-2);}')
88   fi
89
90   ADMINPASSWORD=$(cat $OVERRIDEYAML | awk '/^.*adminPassword:.*/{print $2;}')
91   if [ -z $ADMINPASSWORD ]; then
92     ADMINPASSWORD=$(cat $DIR/../helm/values.yaml | awk '/^.*adminPassword:.*/{print $2;}')
93   fi
94
95 fi
96
97
98
99
100 #echo $DOCKERPASSWORD, $HELMPASSWORD, $ADMINPASSWORD
101
102
103
104
105 DOCKERPORT=$(cat $DIR/../helm/templates/deployment.yaml  | awk '/.*- name: docker.*/{getline; print $2}')
106
107
108 DOCKERREPOSCRIPT="{\"name\":\"docker_changepassword\",\
109     \"type\":\"groovy\",\
110     \"content\":\"security.securitySystem.changePassword('docker',  '$DOCKERPASSWORD')\"}"
111
112
113
114
115 # This line uses the default admin password
116 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -u admin:$NEXUS_ADMIN_PASSWORD -X POST -H "Content-Type: application/json" --data "$DOCKERREPOSCRIPT" http://nexus.$HOSTPOSTFIX:$INGRESS_PORT_NEXUS/service/rest/v1/script)
117
118
119 if [ "${STATUS}" != "204" ];then
120     echo "> script upload failed!"
121 fi
122
123 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -u admin:$NEXUS_ADMIN_PASSWORD -X POST -H 'Content-Type: text/plain' -H 'Accept: application/json' http://nexus.$HOSTPOSTFIX:$INGRESS_PORT_NEXUS/service/rest/v1/script/docker_changepassword/run)
124
125
126 if [ "${STATUS}" == "200" ];then
127     echo "> docker password change succeeded!"
128 else
129     echo "> docker password change failed!"
130 fi
131
132
133 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE -u admin:$NEXUS_ADMIN_PASSWORD http://nexus.$HOSTPOSTFIX:$INGRESS_PORT_NEXUS/service/rest/v1/script/docker_changepassword)
134
135
136 if [ "${STATUS}" != "204" ];then
137     echo "> script deletion failed!"
138 fi
139
140
141
142
143
144
145
146
147
148
149
150
151
152 HELMREPOSCRIPT="{\"name\":\"helm_changepassword\",\
153     \"type\":\"groovy\",\
154     \"content\":\"security.securitySystem.changePassword('helm',  '$HELMPASSWORD')\"}"
155
156
157
158 # This line uses the default admin password
159 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -u admin:$NEXUS_ADMIN_PASSWORD -X POST -H "Content-Type: application/json" --data "$HELMREPOSCRIPT" http://nexus.$HOSTPOSTFIX:$INGRESS_PORT_NEXUS/service/rest/v1/script)
160
161
162 if [ "${STATUS}" != "204" ];then
163     echo "> script upload failed!"
164 fi
165
166 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -u admin:$NEXUS_ADMIN_PASSWORD -X POST -H 'Content-Type: text/plain' -H 'Accept: application/json' http://nexus.$HOSTPOSTFIX:$INGRESS_PORT_NEXUS/service/rest/v1/script/helm_changepassword/run)
167
168
169 if [ "${STATUS}" == "200" ];then
170     echo "> helm password change succeeded!"
171 else
172     echo "> helm password change failed!"
173 fi
174
175
176 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE -u admin:$NEXUS_ADMIN_PASSWORD http://nexus.$HOSTPOSTFIX:$INGRESS_PORT_NEXUS/service/rest/v1/script/helm_changepassword)
177
178
179 if [ "${STATUS}" != "204" ];then
180     echo "> script deletion failed!"
181 fi
182
183
184
185
186
187
188
189
190
191
192
193
194
195 ADMINSCRIPT="{\"name\":\"admin_changepassword\",\
196     \"type\":\"groovy\",\
197     \"content\":\"security.securitySystem.changePassword('admin',  '$ADMINPASSWORD')\"}"
198
199
200
201 # This line uses the default admin password
202 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -u admin:$NEXUS_ADMIN_PASSWORD -X POST -H "Content-Type: application/json" --data "$ADMINSCRIPT" http://nexus.$HOSTPOSTFIX:$INGRESS_PORT_NEXUS/service/rest/v1/script)
203
204
205 if [ "${STATUS}" != "204" ];then
206     echo "> script upload failed!"
207 fi
208
209 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -u admin:$NEXUS_ADMIN_PASSWORD -X POST -H 'Content-Type: text/plain' -H 'Accept: application/json' http://nexus.$HOSTPOSTFIX:$INGRESS_PORT_NEXUS/service/rest/v1/script/admin_changepassword/run)
210
211
212 if [ "${STATUS}" == "200" ];then
213     echo "> admin password change succeeded!"
214 else
215     echo "> admin password change failed!"
216 fi
217
218 NEXUS_ADMIN_PASSWORD=$ADMINPASSWORD
219 STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE -u admin:$NEXUS_ADMIN_PASSWORD http://nexus.$HOSTPOSTFIX:$INGRESS_PORT_NEXUS/service/rest/v1/script/admin_changepassword)
220
221
222 if [ "${STATUS}" != "204" ];then
223     echo "> script deletion failed!"
224 fi
225