Skip to content

Commit

Permalink
feat: add VM identity GCP role
Browse files Browse the repository at this point in the history
  • Loading branch information
meysam81 committed Jan 16, 2025
1 parent 89402d7 commit e21d71a
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 0 deletions.
30 changes: 30 additions & 0 deletions roles/vm_identity_gcp/HEADER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# VM Identity GCP

## Install

### requirements.yml

```yaml
collections:
- name: meysam81.general
version: 1.9.0
```
## Usage
### playbook.yml
```yaml
---
- name: Playbook
gather_facts: true
become: true
hosts: all
tasks:
roles:
- name: meysam81.general.vm_identity_gcp
vars:
vm_identity_gcp_env_vars:
AWS_CONFIG_FILE: /home/ec2-user/.aws/config
vm_identity_gcp_token_audience: sts.amazonaws.com
```
107 changes: 107 additions & 0 deletions roles/vm_identity_gcp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# VM Identity GCP

## Install

### requirements.yml

```yaml
collections:
- name: meysam81.general
version: 1.9.0
```
## Usage
### playbook.yml
```yaml
---
- name: Playbook
gather_facts: true
become: true
hosts: all
tasks:
roles:
- name: meysam81.general.vm_identity_gcp
vars:
vm_identity_gcp_env_vars:
AWS_CONFIG_FILE: /home/ec2-user/.aws/config
vm_identity_gcp_token_audience: sts.amazonaws.com
```
Retrive GCP VM identity token and store it in a file. Best for using in OpenID Connect authentication.
## Table of content
- [Requirements](#requirements)
- [Default Variables](#default-variables)
- [vm_identity_gcp_env_file_path](#vm_identity_gcp_env_file_path)
- [vm_identity_gcp_env_vars](#vm_identity_gcp_env_vars)
- [vm_identity_gcp_executable_path](#vm_identity_gcp_executable_path)
- [vm_identity_gcp_token_audience](#vm_identity_gcp_token_audience)
- [vm_identity_gcp_token_file_path](#vm_identity_gcp_token_file_path)
- [Dependencies](#dependencies)
- [License](#license)
- [Author](#author)
---
## Requirements
- Minimum Ansible version: `2.17`

## Default Variables

### vm_identity_gcp_env_file_path

#### Default value

```YAML
vm_identity_gcp_env_file_path: /etc/default/vm-identity-gcp
```

### vm_identity_gcp_env_vars

#### Default value

```YAML
vm_identity_gcp_env_vars: {}
```

### vm_identity_gcp_executable_path

#### Default value

```YAML
vm_identity_gcp_executable_path: /usr/local/bin/vm-identity-gcp
```

### vm_identity_gcp_token_audience

#### Default value

```YAML
vm_identity_gcp_token_audience: https://accounts.google.com
```

### vm_identity_gcp_token_file_path

#### Default value

```YAML
vm_identity_gcp_token_file_path: /var/lib/vm-identity/token
```



## Dependencies

None.

## License

Apache-2.0

## Author

Meysam Azad
6 changes: 6 additions & 0 deletions roles/vm_identity_gcp/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
vm_identity_gcp_env_file_path: /etc/default/vm-identity-gcp
vm_identity_gcp_env_vars: {}
vm_identity_gcp_executable_path: /usr/local/bin/vm-identity-gcp
vm_identity_gcp_token_audience: https://accounts.google.com
vm_identity_gcp_token_file_path: /var/lib/vm-identity/token
9 changes: 9 additions & 0 deletions roles/vm_identity_gcp/files/vm-identity-gcp.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Timer for VM Identity Token Retrieval

[Timer]
OnCalendar=*:0/30
Persistent=true

[Install]
WantedBy=timers.target
1 change: 1 addition & 0 deletions roles/vm_identity_gcp/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
26 changes: 26 additions & 0 deletions roles/vm_identity_gcp/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#SPDX-License-Identifier: MIT-0

Check failure on line 1 in roles/vm_identity_gcp/meta/main.yml

View workflow job for this annotation

GitHub Actions / ansible-lint

meta-no-tags

Tags must contain lowercase letters and digits only., invalid: 'service-account'

Check failure on line 1 in roles/vm_identity_gcp/meta/main.yml

View workflow job for this annotation

GitHub Actions / ansible-lint

yaml[comments]

Missing starting space in comment
galaxy_info:
author: Meysam Azad
description: >-
Retrive GCP VM identity token and store it in a file. Best for using in
OpenID Connect authentication.
company: Self employed

license: Apache-2.0

min_ansible_version: "2.17"

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:

galaxy_tags:
- gcp
- vm
- identity
- service-account
- metadata

dependencies:
[]
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.
42 changes: 42 additions & 0 deletions roles/vm_identity_gcp/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
- name: Copy the environment file
ansible.builtin.template:
src: configs.env.j2
dest: "{{ vm_identity_gcp_env_file_path }}"
owner: root
group: root
mode: "0640"
- name: Ensure token dir exists
ansible.builtin.file:
path: "{{ vm_identity_gcp_token_file_path | dirname }}"
owner: root
group: root
state: directory
mode: "0755"
- name: Copy the vm-identity-gcp.timer systemd unit file
ansible.builtin.copy:
src: vm-identity-gcp.timer
dest: /etc/systemd/system/vm-identity-gcp.timer
owner: root
group: root
mode: "0644"
- name: Copy the vm-identity-gcp.service systemd unit file
ansible.builtin.template:
src: vm-identity-gcp.service.j2
dest: /etc/systemd/system/vm-identity-gcp.service
owner: root
group: root
mode: "0644"
- name: Copy the vm-identity-gcp shell script
ansible.builtin.template:
src: vm-identity-gcp.sh.j2
dest: "{{ vm_identity_gcp_executable_path }}"
owner: root
group: root
mode: "0755"
- name: Start and enable the vm-identity-gcp.timer systemd unit
ansible.builtin.systemd:
name: vm-identity-gcp.timer
enabled: true
daemon_reload: true
state: started
3 changes: 3 additions & 0 deletions roles/vm_identity_gcp/templates/configs.env.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% for key, value in vm_identity_gcp_env_vars.items() %}
{{ key }}={{ value }}
{% endfor %}
17 changes: 17 additions & 0 deletions roles/vm_identity_gcp/templates/vm-identity-gcp.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=GCP VM Identity Token Retrieval Service
After=network.target

[Service]
Type=oneshot
ExecStart={{ vm_identity_gcp_executable_path }}
EnvironmentFile={{ vm_identity_gcp_env_file_path }}
PrivateTmp=true
ProtectSystem=full
ProtectHome=yes
NoNewPrivileges=true
ReadOnlyPaths=/
ReadWritePaths={{ vm_identity_gcp_token_file_path | dirname }}

[Install]
WantedBy=multi-user.target
5 changes: 5 additions & 0 deletions roles/vm_identity_gcp/templates/vm-identity-gcp.sh.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh

set -eux

curl -s "http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience={{ vm_identity_gcp_token_audience }}" -HMetadata-Flavor:Google -o {{ vm_identity_gcp_token_file_path }}

0 comments on commit e21d71a

Please sign in to comment.