Skip to content

Commit bc6c0a6

Browse files
feat: wif authentication for google buckets (#1254)
* feat: WIP authentication for GCS * Fix code issue and apply recommended change
1 parent d2db509 commit bc6c0a6

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

actions/login-to-gcs/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ $ gcloud storage cp OBJECT_LOCATION gs://DESTINATION_BUCKET_NAME
3838

3939
## Inputs
4040

41-
| Name | Type | Description |
42-
| ----------------- | ------ | ----------------------------------------------------------------------------------------------------------------- |
43-
| `bucket` | String | Name of bucket to upload to. Will default to grafanalabs-${repository.name}-${environment} |
44-
| `environment` | String | Environment for pushing artifacts (can be either dev or prod). |
45-
| `service_account` | String | Service account to use for authentication. Use it only when the service account is different than the default one |
41+
| Name | Type | Description |
42+
| ----------------- | ------- | ----------------------------------------------------------------------------------------------------------------- |
43+
| `bucket` | String | Name of bucket to upload to. Will default to grafanalabs-${repository.name}-${environment} |
44+
| `environment` | String | Environment for pushing artifacts (can be either dev or prod). |
45+
| `service_account` | String | Service account to use for authentication. Use it only when the service account is different than the default one |
46+
| `use_wif_auth` | Boolean | Use WIF authentication. Overrides the `service_account` input. |
4647

4748
## Outputs
4849

actions/login-to-gcs/action.yaml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ inputs:
1818
Delete the credentials file after the action is finished.
1919
If you want to keep the credentials file for a later step, set this to false.
2020
default: "false"
21+
use_wif_auth:
22+
description: |
23+
Use WIF for authentication instead of service account.
24+
required: false
25+
default: "false"
2126

2227
outputs:
2328
bucket:
@@ -58,20 +63,25 @@ runs:
5863
fi
5964
echo "bucket=${BUCKET}" | tee -a ${GITHUB_OUTPUT}
6065
61-
# Construct service account
62-
if [[ "${SERVICE_ACCOUNT}" == "" ]]; then
63-
SERVICE_ACCOUNT="github-${{ github.repository_id }}-${ENVIRONMENT}[email protected]"
66+
# set up a service account only if we're NOT using WIF auth.
67+
if [[ "${USE_WIF_AUTH}" != "true" ]]; then
68+
# Construct service account
69+
if [[ "${SERVICE_ACCOUNT}" == "" ]]; then
70+
SERVICE_ACCOUNT="github-${{ github.repository_id }}-${ENVIRONMENT}[email protected]"
71+
fi
72+
echo "service_account=${SERVICE_ACCOUNT}" | tee -a ${GITHUB_OUTPUT}
6473
else
65-
SERVICE_ACCOUNT="${SERVICE_ACCOUNT}"
74+
echo "service_account=" | tee -a ${GITHUB_OUTPUT}
6675
fi
67-
echo "service_account=${SERVICE_ACCOUNT}" | tee -a ${GITHUB_OUTPUT}
6876
env:
6977
BUCKET: ${{ inputs.bucket }}
7078
ENVIRONMENT: ${{ inputs.environment }}
7179
SERVICE_ACCOUNT: ${{ inputs.service_account }}
80+
USE_WIF_AUTH: ${{ inputs.use_wif_auth }}
7281
- uses: google-github-actions/auth@7c6bc770dae815cd3e89ee6cdf493a5fab2cc093 # v3.0.0
7382
id: gcloud-auth
7483
with:
84+
project_id: "grafanalabs-workload-identity"
7585
workload_identity_provider: "projects/304398677251/locations/global/workloadIdentityPools/github/providers/github-provider"
7686
service_account: ${{ steps.construct-account-vars.outputs.service_account }}
7787
- name: Delete Google Application Credentials file

actions/push-to-gcs/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ jobs:
126126
| `parent` | String | Whether parent dir should be included in GCS destination. Dirs included in the `glob` statement are unaffected by this setting. |
127127
| `predefinedAcl` | String | Predefined ACL applied to the uploaded objects. Default is `projectPrivate`. See [Google Documentation][gcs-docs-upload-options] for a list of available options. |
128128
| `delete_credentials_file` | Boolean | Delete the credentials file after the action is finished. If you want to keep the credentials file for a later step, set this to false. (Default: `true`) |
129+
| `use_wif_auth` | Boolean | Use WIF authentication. Overrides the `service_account` input. |
130+
131+
> [!TIP]
132+
> To use WIF authentication you must enable `uniform_bucket_level_access` on the destination bucket. If you are at Grafana Labs, instructions can be found [here](https://enghub.grafana-ops.net/docs/default/component/deployment-tools/platform/continuous-integration/google-artifact-registry/). More info can be found in [Google's docs](https://cloud.google.com/storage/docs/uniform-bucket-level-access).
129133

130134
## Outputs
131135

actions/push-to-gcs/action.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ inputs:
4545
description: |
4646
If true, then upload files with `content-encoding: gzip`
4747
default: "true"
48+
use_wif_auth:
49+
description: |
50+
Use WIF for authentication instead of service account.
51+
required: false
52+
default: "false"
4853

4954
outputs:
5055
uploaded:
@@ -87,6 +92,7 @@ runs:
8792
environment: ${{ inputs.environment }}
8893
service_account: ${{ inputs.bucket && inputs.service_account || '' }}
8994
delete_credentials_file: false
95+
use_wif_auth: ${{ inputs.use_wif_auth }}
9096
- name: Construct path
9197
id: construct-path
9298
shell: bash
@@ -106,7 +112,7 @@ runs:
106112
glob: ${{ inputs.glob }}
107113
destination: ${{ steps.construct-path.outputs.destination }} # bucket name plus folder prefix (if applicable)
108114
parent: ${{ inputs.parent }}
109-
predefinedAcl: ${{ inputs.predefinedAcl }}
115+
predefinedAcl: ${{ inputs.use_wif_auth == 'true' && ' ' || inputs.predefinedAcl }} # when using WIF auth, we cannot use predefinedAcl
110116
gzip: ${{ inputs.gzip }}
111117
process_gcloudignore: false
112118

0 commit comments

Comments
 (0)