Skip to content

Commit 8104a24

Browse files
dploegerDennis Ploeger
authored andcommitted
feat: Made non-sa-auth possible with gcloud
1 parent 922aa36 commit 8104a24

7 files changed

Lines changed: 97 additions & 24 deletions

File tree

ccc/ccc.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,24 @@ func main() {
327327
if err := file.Close(); err != nil {
328328
fatal(err)
329329
}
330+
})
330331

332+
api.POST("/googleAuth", func(c *gin.Context) {
333+
var json Mfa
334+
if err := c.ShouldBindJSON(&json); err != nil {
335+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
336+
return
337+
}
338+
file, err := os.Create("/tmp/gcloud_auth")
339+
if err != nil {
340+
fatal(err)
341+
}
342+
if _, err := file.WriteString(fmt.Sprintf("%s\n", json.Code)); err != nil {
343+
fatal(err)
344+
}
345+
if err := file.Close(); err != nil {
346+
fatal(err)
347+
}
331348
})
332349

333350
if err := router.Run(fmt.Sprintf(":%s", port)); err != nil {

ccc/client/src/components/Progress.vue

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@
1414
Open Authentication
1515
</v-btn>
1616
</v-alert>
17-
<v-alert :value="oAuthCode === '' && oAuthUrl !== ''" type="info">
18-
CloudControlCenter has detected an authentication request. Click here to open the authentication URL:
17+
<v-alert :value="googleAuth && !completedAuth" type="info">
18+
CloudControlCenter has detected a google authentication request. After you've clicked the following
19+
button and authenticated in, copy the Google authorization code you'll get into the text input below.
1920
<v-btn v-on:click="doOAuth">
2021
Open Authentication
2122
</v-btn>
23+
<v-form v-on:submit="sendGoogleAuth">
24+
<v-text-field autofocus v-model="googleAuthCode"></v-text-field>
25+
<v-btn type="submit">Send code</v-btn>
26+
</v-form>
2227
</v-alert>
2328
<v-alert :value="requiresMFA" type="info">
2429
CloudControlCenter has detected an MFA code request. Enter the current code of your authenticator:
@@ -83,8 +88,12 @@ export default class Progress extends Vue {
8388
public currentError: string = '';
8489
8590
public requiresMFA: boolean = false;
91+
public googleAuth: boolean = false;
92+
public googleAuthCode: string = '';
8693
public mfaCode: string = '';
8794
95+
public completedAuth: boolean = false;
96+
8897
public mounted() {
8998
axios.default.get('/api/steps')
9099
.then(
@@ -151,13 +160,12 @@ export default class Progress extends Vue {
151160
}
152161
}
153162
154-
const googleOauthRegexp = new RegExp(
155-
'Your browser has been opened to visit:\n\n\s+(.+)$'
156-
);
163+
const googleOauthRegexp = new RegExp('Go to the following link in your browser:\r<br/>\r<br/> +(.+)')
157164
if (googleOauthRegexp.test(output)) {
158165
const matches = googleOauthRegexp.exec(output);
159166
if (matches) {
160167
this.oAuthUrl = matches[1];
168+
this.googleAuth = true;
161169
}
162170
}
163171
// MFA feature. Check for a regexp request, but also check if the MFA was already entered.
@@ -193,6 +201,29 @@ export default class Progress extends Vue {
193201
});
194202
195203
}
204+
205+
public sendGoogleAuth(event: Event) {
206+
event.preventDefault();
207+
axios.default.post('/api/googleAuth', {
208+
code: this.googleAuthCode,
209+
})
210+
.then(() => {
211+
this.googleAuth = false;
212+
this.googleAuthCode = '';
213+
this.currentError = '';
214+
this.completedAuth = true;
215+
})
216+
.catch((error) => {
217+
this.currentError = 'Can not set Google Auth code:';
218+
if (error.response) {
219+
this.currentError = `${this.currentError} ([${error.response.status}] ${error.response.data})`;
220+
} else if (error.message) {
221+
this.currentError = `${this.currentError} (${error.message})`;
222+
}
223+
});
224+
225+
}
226+
196227
}
197228
</script>
198229

feature/kubernetes/install.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ then
192192
do
193193
ZONE=$(echo "${ZONEDCLUSTER}" | cut -d ":" -f 1)
194194
CLUSTER=$(echo "${ZONEDCLUSTER}" | cut -d ":" -f 2)
195-
execHandle "Authenticating against cluster ${CLUSTER} in zone ${ZONE}" gcloud container clusters get-credentials "${CLUSTER}" --zone "${ZONE}" --project "${GCLOUD_PROJECTID}"
196-
echo gcloud container clusters get-credentials "${CLUSTER}" --zone "${ZONE}" --project "${GCLOUD_PROJECTID}" >> ~/bin/k8s-relogin
195+
execHandle "Authenticating against cluster ${CLUSTER} in zone ${ZONE}" gcloud container clusters get-credentials "${CLUSTER}" --zone "${ZONE}"
196+
echo gcloud container clusters get-credentials "${CLUSTER}" --zone "${ZONE}" >> ~/bin/k8s-relogin
197197
chmod +x ~/bin/k8s-relogin
198198
done
199199
fi

flavour/gcloud/Dockerfile.flavour

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ RUN apk add sudo bash curl && \
1414

1515
COPY flavour/gcloud/flavour /home/cloudcontrol/flavour
1616
COPY flavour/gcloud/flavourinit.sh /home/cloudcontrol/bin/flavourinit.sh
17+
COPY flavour/gcloud/login.expect /home/cloudcontrol/bin/login.expect
1718
RUN chmod +x /home/cloudcontrol/bin/flavourinit.sh

flavour/gcloud/flavour.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ description: |
1010
* Create a key and download it as a JSON file
1111
* Mount a directory that contains the JSON file into the CloudControl container and set GCLOUD_KEYPATH accordingly
1212
configuration:
13-
- "Environment GCLOUD_PROJECTID: The id of the Google Cloud project to connect to"
14-
- "Environment GCLOUD_USE_SA (Possible values: true, false. Defaults to false): Use a service account to log into Google Cloud. Requires GCLOUD_KEYPATH"
15-
- "Environment GCLOUD_KEYPATH: Path inside CloudControl that holds the service account JSON file"
13+
- "Environment GOOGLE_PROJECT: The id of the Google Cloud project to connect to"
14+
- |
15+
Environment GOOGLE_CREDENTIALS: Path inside CloudControl that holds the service account JSON file. Will use
16+
browser based login if unset.
1617
platforms:
1718
- linux/amd64
1819
- linux/arm64

flavour/gcloud/flavourinit.sh

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,35 @@
22

33
. /feature-installer-utils.sh
44

5-
if [ "X${GCLOUD_USE_SA:-false}X" == "XtrueX" ] && [ -z "$GCLOUD_KEYPATH" ]
5+
if [ "X${GCLOUD_USE_SA:-false}X" == "XtrueX" ]
66
then
7-
echo "Please set GCLOUD_KEYPATH environment variable"
8-
exit 1
7+
echo "Use of GCLOUD_USE_SA is deprecated, please simply set GOOGLE_CREDENTIALS to the path of a service account"
8+
echo "key file."
9+
if [ -z "${GCLOUD_KEYPATH}" ]
10+
then
11+
echo "GCLOUD_USE_SA was enabled, but GCLOUD_KEYPATH was not set."
12+
exit 1
13+
fi
14+
GOOGLE_CREDENTIALS="${GCLOUD_KEYPATH}"
915
fi
1016

11-
if [ "X${GCLOUD_USE_SA:-false}X" == "XtrueX" ] && [ ! -r "$GCLOUD_KEYPATH" ]
17+
if [ -n "${GOOGLE_CREDENTIALS}" ]
1218
then
13-
echo "File ${GCLOUD_KEYPATH} is not readable"
14-
exit 1
19+
execHandle "Authenticating service account" gcloud auth activate-service-account --key-file "${GOOGLE_CREDENTIALS}"
20+
else
21+
execHandle "Installing expect" sudo apk add expect
22+
expect /home/cloudcontrol/bin/login.expect
1523
fi
1624

17-
if [ -z "$GCLOUD_PROJECTID" ]
25+
if [ -n "${GCLOUD_PROJECTID}" ]
1826
then
19-
echo "Please set GCLOUD_PROJECTID environment variable"
20-
exit 1
27+
echo "Usage of GCLOUD_PROJECTID is deprecated. Please use GOOGLE_PROJECT instead."
28+
GOOGLE_PROJECT="${GCLOUD_PROJECTID}"
2129
fi
2230

23-
if [ "X${GCLOUD_USE_SA:-false}X" == "XtrueX" ]
31+
if [ -n "${GOOGLE_PROJECT}" ]
2432
then
25-
execHandle "Authenticating service account" gcloud auth activate-service-account --key-file "$GCLOUD_KEYPATH"
26-
else
27-
execHandle "Authenticating service account" gcloud auth login
33+
execHandle "Setting project" gcloud config set project "${GOOGLE_PROJECT}"
2834
fi
29-
execHandle "Setting project" gcloud config set project "$GCLOUD_PROJECTID"
3035

3136
exit 0

flavour/gcloud/login.expect

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
proc wait_for_auth_code {} {
2+
if {[file exists /tmp/gcloud_auth]} {
3+
set fp [open "/tmp/gcloud_auth" r]
4+
set file_data [read $fp]
5+
close $fp
6+
return $file_data
7+
} else {
8+
after 3000
9+
wait_for_auth_code
10+
}
11+
}
12+
13+
set timeout 90000
14+
spawn gcloud auth login --no-launch-browser
15+
expect "Enter authorization code"
16+
set auth_code [wait_for_auth_code]
17+
send $auth_code
18+
expect eof

0 commit comments

Comments
 (0)