Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Tuesday, November 24, 2026

Okta as Code, Part 2

Storing Your Terraform Code in GitLab

In Part 1 I put an Okta org under Terraform on a Debian server. In this part I store that code in GitLab, so it is versioned, backed up and ready to share. Every command is followed by a short explanation, and every code box has a Copy button.

How this setup works: GitLab stores the code. Terraform still runs locally on the server: I edit the files, run terraform plan and terraform apply myself, and then commit and push the result to GitLab. The push does not change Okta. A pipeline that runs Terraform from GitLab is a later step.
Placeholders: replace it-systems-theo, Terraform-integrator-4574553 and integrator-4574553 with your own GitLab group, project and Okta org names. The project must be private, and no keys or credentials ever go into the repo.

Step 1: Create an SSH key

GitLab needs to know that pushes come from you. An SSH key pair does that without typing a password each time.

ssh-keygen -t ed25519 -C "theo@home-theo" -f ~/.ssh/id_ed25519_gitlab

This creates a key pair. -t ed25519 is a modern, short key type, -C is just a label, and -f sets the file name. It asks for a passphrase: choose one, because it protects the key if the file is ever copied.

Output of ssh-keygen

The fingerprint and the random square of characters that appear are safe to share. The private key file (id_ed25519_gitlab, without .pub) and your passphrase never are.

Print the public key and copy the whole line, which starts with ssh-ed25519:

cat ~/.ssh/id_ed25519_gitlab.pub

Step 2: Add the public key to GitLab

In GitLab, click your avatar (top left), then Edit profile.

GitLab avatar menu and Edit profile

Go to SSH Keys and click Add new key. Paste the public key, set the title to home-theo, and click Add key.

Adding a new SSH key in GitLab
The SSH key added in GitLab

Step 3: Tell SSH to use the key for GitLab

nano ~/.ssh/config

This opens (or creates) the file that controls how SSH connects to each server. Paste this, then save with Ctrl+O, Enter, and exit with Ctrl+X:

Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_ed25519_gitlab
  IdentitiesOnly yes

Whenever you connect to gitlab.com, SSH logs in as the user git and uses only this key.

Restrict the file so only you can read it:

chmod 600 ~/.ssh/config

Test the connection:

ssh -T git@gitlab.com

The first time, answer yes to the fingerprint question and enter your passphrase if you set one. Success looks like Welcome to GitLab, @yourusername!

Successful SSH test against GitLab

Step 4: Create the project in GitLab

Click New project, then Create blank project.

Creating a blank project in GitLab

Set Visibility Level to Private.

Untick “Initialize repository with a README”. Your local repository already has commits, and a README would make the first push fail.
Project settings with Private visibility and no README

After you click Create project, GitLab shows instructions for an empty project. Use the section Push an existing Git repository. Ignore the other two, because they would create a second repository.

GitLab instructions for an empty project

Step 5: Link your local repository and push

Go to the local repository and set the name and email recorded on your commits, for this repo only:

cd ~/okta-terraform
git config --local user.name "Your Name"
git config --local user.email "you@example.com"

Link the local repository to the GitLab project. Copy the address from Code > Clone with SSH in GitLab:

git remote add origin git@gitlab.com:it-systems-theo/Terraform-integrator-4574553.git

This tells Git that the GitLab project is called origin. Check it:

git remote -v

It lists two lines, one for fetch and one for push, with the same address.

Before you push anything, run git ls-files. It lists every tracked file, and you should see only .gitignore, your .tf files and .terraform.lock.hcl. If a .pem, .env or .tfstate file appears, stop and remove it from Git first.
git ls-files

Push your main branch:

git push --set-upstream origin main

This uploads your commits. --set-upstream remembers the link, so later a plain git push is enough. If your key has a passphrase, it asks for it now (nothing shows as you type).

Output of git push

Step 6: Make a change: add a group and a rule

Now the real workflow. Edit the Terraform code, apply it locally, then store the change in GitLab.

Move to the Terraform folder. This is not the project root:

cd ~/okta-terraform/envs/okta-integrator-4574553

Open main.tf (or edit it in VS Code):

nano main.tf

Add this at the very bottom, below the existing blocks. Don't delete anything:

resource "okta_group" "it_support" {
  name        = "IT Support"
  description = "Managed by Terraform"
}

resource "okta_group_rule" "it_support" {
  name              = "IT Support by department"
  status            = "ACTIVE"
  group_assignments = [okta_group.it_support.id]
  expression_type   = "urn:okta:expression:1.0"
  expression_value  = "user.department==\"IT\""
}

The first block creates a group named IT Support. The second block automatically adds users whose department equals IT to that group. It refers to the group with okta_group.it_support.id, so Terraform creates the group first.

Step 7: Check, plan and apply locally

Load your Okta credentials, tidy the formatting and check the syntax:

source ~/.okta/okta-integrator-4574553.env
terraform fmt
terraform validate

source loads your credentials into this terminal. terraform fmt formats the files. terraform validate checks the syntax without contacting Okta, and you want Success! The configuration is valid.

Preview the changes:

terraform plan

This logs in to Okta and shows what would change, without changing anything. A good result ends with Plan: 2 to add, 0 to change, 0 to destroy.

Output of terraform plan

Apply the changes:

terraform apply

Type yes when asked. It should end with Apply complete! Resources: 2 added, 0 changed, 0 destroyed. Then check Directory > Groups in the Okta Admin Console.

Output of terraform apply

Step 8: Commit and push the change to GitLab

Only now does the change go to GitLab, as a record of what you applied. Go to the project root and check what changed:

cd ~/okta-terraform
git status

git status should list only main.tf as modified. Stage it by name, commit it and push it:

git add envs/okta-integrator-4574553/main.tf
git commit -m "Add IT Support group and rule"
git push
  • git add stages the file
  • git commit records it in local history with a message
  • git push uploads the commit to GitLab

Your daily routine from now on:

git status
git add FILENAME
git commit -m "What changed"
git push

What GitLab does and does not do here

  • GitLab: stores and versions the code, so you have history and a backup.
  • The server: runs Terraform, holds the credentials in ~/.okta and keeps the state file.
  • You: are the automation. A push to GitLab does not change Okta.

Next: a GitLab pipeline that runs terraform plan on merge requests and apply on main. That needs remote state, a runner and the Okta credentials stored as CI variables, so I'll cover it in the next part.