Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow application default credentials when running integration tests #601

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 69 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,89 @@ under a directory `ansible_collections`. Clone ensuring that hierarchy:

```shell
mkdir -p $TARGET_DIR/ansible_collections/google
git clone <url> $TARGET_DIR/collections/google/cloud
git clone <url> $TARGET_DIR/ansible_collections/google/cloud
```

Then set up your Python virtual environment:

```shell
cd $TARGET_DIR/ansible_collections/google
python3 -m venv venv
. ./venv/bin/activate
pip3 install -r requirements.txt
pip3 install -r requirements-test.txt
pip3 install ansible
```

## Running tests

### prequisites for all tests
### Prequisites for all tests

- Install `gcloud` following [these instructions](https://cloud.google.com/sdk/docs/install).
- Install the `ansible` package.
- Some container runtime is necessary (e.g. `podman` or `docker`). The instructions use podman.

## Running integration tests

### Integration testing prequisites

#### Installing personal GCP credentials
#### Authentication with personal GCP credentials

If you are running the integration tests locally the easiest way to
authenticate to GCP is using [application default credentials](https://cloud.google.com/sdk/docs/authorizing#adc).
Once you have installed `gcloud` and performed basic initialization (via `gcloud init`) run:

```shell
gcloud auth application-default login
```

#### Authentication with service account credentials

A service account may also be used to run the integration tests. You can create one using `gcloud`:

```shell
gcloud iam service-accounts create ansible-test-account \
--description="For running Anisble integration tests" \
--display-name="Ansible Test Account"
```

You'll also need to export a key file. Here and below `$SERVICE_ACCOUNT_NAME`
is the full email address of the service account, in the form
`EMAIL@PROJECT_ID.iam.gserviceaccount.com`, e.g., if you used the
account name `ansible-test-account` as suggested above and your project
ID is `my-test-project`, use `[email protected]`.

```shell
gcloud iam service-accounts keys create /path/to/cred/file.json \
--iam-account=ansible-test-account@my-test-project.iam.gserviceaccount.com
chmod 0600 /path/to/cred/file.json
```

Read the [best practices for managing service account keys](https://cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys)
to learn how to keep your service account key and your GCP resources safe.

#### Configuring test credentials

The integration tests for this module require the use of real GCP credentials, and must provide
ansible-test those values. They can be added by authoring the following in `tests/integration/cloud-config-gcp.ini`:
ansible-test those values. They can be added by creating the file `tests/integration/cloud-config-gcp.ini`.

If you are using personal (i.e., application default) credentials, add:

```
[default]
gcp_project: $PROJECT_ID
gcp_cred_kind: application
gcp_folder_id: $TEST_FOLDER (to create test projects)
```

If you are using a service account for credentials, add:

```
[default]
gcp_project: @PROJECT_ID
gcp_cred_file: @CRED_FILE
gcp_cred_kind: @CRED_KIND
gcp_cred_email: @EMAIL
gcp_folder_id: @TEST_FOLDER (to create test projects)
gcp_project: $PROJECT_ID
gcp_cred_file: /path/to/cred/file.json
gcp_cred_kind: serviceaccount
gcp_folder_id: $TEST_FOLDER (to create test projects)
```

#### Setting up the project for testing
Expand All @@ -51,7 +108,8 @@ and is expected to be configured beforehand.
For convenience, a bootstrap script is provided.

NOTE: running this script will make irreversible changes in your
GCP project (e.g. create an AppEngine project):
GCP project (e.g. create an AppEngine project). You can omit
`$SERVICE_ACCOUNT_NAME` is you are using application default credentials.

```bash
bash ./scripts/bootstrap-project.sh $PROJECT_ID $SERVICE_ACCOUNT_NAME
Expand Down Expand Up @@ -92,7 +150,7 @@ ansible-lint

## Specific Tasks

The following enumerates detailed documentation for specific tasks related tot
The following enumerates detailed documentation for specific tasks related to
the codebase.

### Updating the supported ansible-core version
Expand Down
2 changes: 2 additions & 0 deletions changelogs/fragments/app-default-creds.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ansible-test - add support for GCP application default credentials (https://github.com/ansible-collections/google.cloud/issues/359).
15 changes: 9 additions & 6 deletions scripts/bootstrap-project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ for SERVICE in "${SERVICE_LIST[@]}"; do
gcloud services enable "$SERVICE" --project="$PROJECT_ID"
done

for ROLE in "${REQUIRED_ROLE_LIST[@]}"; do
echo "enabling role $ROLE..."
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
--member="serviceAccount:$SERVICE_ACCOUNT_NAME" \
--role="$ROLE"
done
if [ -n "$SERVICE_ACCOUNT_NAME" ]
then
for ROLE in "${REQUIRED_ROLE_LIST[@]}"; do
echo "enabling role $ROLE..."
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
--member="serviceAccount:$SERVICE_ACCOUNT_NAME" \
--role="$ROLE"
done
fi

if ! gcloud app describe --project="$PROJECT_ID" > /dev/null; then
echo "creating appengine project..."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
resource_name: "{{ resource_prefix }}"
resource_name: "{{ resource_prefix }}"
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
action: ALLOW
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: absent
#----------------------------------------------------------
- name: create a firewall rule
Expand All @@ -30,7 +30,7 @@
action: ALLOW
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: present
register: result
- name: assert changed is true
Expand All @@ -41,7 +41,7 @@
google.cloud.gcp_appengine_firewall_rule_info:
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
scopes:
- https://www.googleapis.com/auth/cloud-platform
register: results
Expand All @@ -57,7 +57,7 @@
action: ALLOW
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: present
register: result
- name: assert changed is false
Expand All @@ -72,7 +72,7 @@
action: ALLOW
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: absent
register: result
- name: assert changed is true
Expand All @@ -83,7 +83,7 @@
google.cloud.gcp_appengine_firewall_rule_info:
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
scopes:
- https://www.googleapis.com/auth/cloud-platform
register: results
Expand All @@ -101,7 +101,7 @@
action: ALLOW
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: absent
register: result
- name: assert changed is false
Expand Down
14 changes: 7 additions & 7 deletions tests/integration/targets/gcp_bigquery_dataset/tasks/autogen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
dataset_id: my_example_dataset
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: absent
#----------------------------------------------------------
- name: create a dataset
Expand All @@ -30,7 +30,7 @@
dataset_id: my_example_dataset
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: present
register: result
- name: assert changed is true
Expand All @@ -41,7 +41,7 @@
google.cloud.gcp_bigquery_dataset_info:
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
scopes:
- https://www.googleapis.com/auth/bigquery
register: results
Expand All @@ -57,7 +57,7 @@
dataset_id: my_example_dataset
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: present
register: result
- name: assert changed is false
Expand All @@ -72,7 +72,7 @@
dataset_id: my_example_dataset
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: absent
register: result
- name: assert changed is true
Expand All @@ -83,7 +83,7 @@
google.cloud.gcp_bigquery_dataset_info:
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
scopes:
- https://www.googleapis.com/auth/bigquery
register: results
Expand All @@ -99,7 +99,7 @@
dataset_id: my_example_dataset
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: absent
register: result
- name: assert changed is false
Expand Down
18 changes: 9 additions & 9 deletions tests/integration/targets/gcp_bigquery_table/tasks/autogen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
dataset_id: example_dataset
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: present
register: dataset
- name: delete a table
Expand All @@ -33,7 +33,7 @@
table_id: example_table
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: absent
#----------------------------------------------------------
- name: create a table
Expand All @@ -46,7 +46,7 @@
table_id: example_table
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: present
register: result
- name: assert changed is true
Expand All @@ -58,7 +58,7 @@
dataset: example_dataset
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
scopes:
- https://www.googleapis.com/auth/bigquery
register: results
Expand All @@ -77,7 +77,7 @@
table_id: example_table
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: present
register: result
- name: assert changed is false
Expand All @@ -95,7 +95,7 @@
table_id: example_table
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: absent
register: result
- name: assert changed is true
Expand All @@ -107,7 +107,7 @@
dataset: example_dataset
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
scopes:
- https://www.googleapis.com/auth/bigquery
register: results
Expand All @@ -126,7 +126,7 @@
table_id: example_table
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: absent
register: result
- name: assert changed is false
Expand All @@ -143,7 +143,7 @@
dataset_id: example_dataset
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
service_account_file: "{{ gcp_cred_file | default(omit) }}"
state: absent
register: dataset
ignore_errors: true
Loading
Loading