sommerfeld-io / .github

Special repository with fallback content for all of repos that don't have an actual .github directory
0 stars 0 forks source link

Manage GitHub Organization Access via Infrastructure as Code #25

Open sebastian-sommerfeld-io opened 1 month ago

sebastian-sommerfeld-io commented 1 month ago

As the owner of the sommerfeld.io GitHub organization and maintainer of its repositories, I want to manage user access to the organization and all its repositories as infrastructure-as-code, so that access control is consistent, auditable, and automated.

Acceptance Criteria

Optional

Alternative

sebastian-sommerfeld-io commented 1 month ago

⚠️ ⚠️ ⚠️ This information comes from ChatGPT - I am not sure if this is really correct ⚠️ ⚠️ ⚠️


To implement access management for your GitHub organization using Ansible, you can leverage the community.general.github_user and community.general.github_team modules, which integrate with the GitHub API to manage users and teams. Here's a step-by-step guide to create a solution for managing user access:

1. Set up GitHub Personal Access Token

You need a GitHub personal access token (PAT) with the necessary scopes to manage the organization and repository access:

2. Install Required Collections

Ensure that you have the community.general collection installed, which contains the GitHub modules:

ansible-galaxy collection install community.general

3. Define Your Playbook

Your playbook should handle:

Here’s an example Ansible playbook for managing access:

Example: github_access.yml

---
- name: Manage GitHub organization access
  hosts: localhost
  gather_facts: false
  vars:
    github_token: "{{ lookup('env', 'GITHUB_TOKEN') }}"  # Secure your token using environment variables
    github_organization: "sommerfeld-io"
    users:
      - { login: "user1", access_level: "admin", repos: ["repo1", "repo2"] }
      - { login: "user2", access_level: "write", repos: ["repo1"] }
      - { login: "user3", access_level: "read", repos: ["repo2"] }

  tasks:
    - name: Ensure users are part of the organization
      community.general.github_membership:
        organization: "{{ github_organization }}"
        login: "{{ item.login }}"
        role: "{{ item.access_level }}"
        state: present
        token: "{{ github_token }}"
      loop: "{{ users }}"

    - name: Manage repository access for users
      community.general.github_collaborator:
        organization: "{{ github_organization }}"
        repository: "{{ repo }}"
        username: "{{ item.login }}"
        permission: "{{ item.access_level }}"
        state: present
        token: "{{ github_token }}"
      loop: "{{ users }}"
      loop_control:
        loop_var: item
      vars:
        repo: "{{ item.repos }}"

    - name: Revoke access from users
      community.general.github_membership:
        organization: "{{ github_organization }}"
        login: "{{ item.login }}"
        state: absent
        token: "{{ github_token }}"
      loop:
        - { login: "user_to_remove" }  # Add users to remove here

4. Environment Variables

Store your GitHub token securely by setting it as an environment variable. This avoids storing sensitive information directly in your playbook:

export GITHUB_TOKEN=your_github_personal_access_token

5. Playbook Breakdown

6. Running the Playbook

To apply your access controls:

ansible-playbook github_access.yml

7. Automating Audits

Ansible keeps logs of actions taken, so you can review them to verify that user access is being managed according to the defined playbook. Additionally, GitHub’s own audit logs can help you trace any access changes.

By running this playbook regularly (perhaps via a CI pipeline), you can ensure that access levels remain consistent and aligned with the desired state defined in your Ansible code.