# GitLab Setup

### Module Objective

* Setting up a GitLab server
* Creating users
* Pushing and Pulling repositories from our private instance
* Creating build pipelines to test for failures

### Prerequisites

{% code overflow="wrap" %}

```yaml
Minimum:
- 4GB RAM
- 50GB Disk Space
- Virtualisation Software (VMWare or VirtualBox)
- Linux Server (We will be using Ubuntu 22.04 for our lab)
    - https://ubuntu.com/download/server
```

{% endcode %}

#### Network Setup (OPTIONAL)

{% hint style="info" %}
**Section Objective**

* Create a segregated network for private connections
  {% endhint %}

This section goes over how to set up a Host-Only network if you want to access the server from a virtual machine that is not connected to NAT.

On the **Virtual Network Editor**, click on ***Add Network -> OK*** then select the **Host-Only** option. You can rename it if you would like.

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2FCs3CIpXDxDOyOKlC8JaV%2Fimage.png?alt=media&#x26;token=504638c6-e25d-47ca-a0f8-0982c44c7e26" alt=""><figcaption><p>VMWare Network Editor</p></figcaption></figure>

After you're done with the network configuration, click on your Ubuntu Server VM and click on ***VM -> Settings***

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2FsYbpdwnoujjhcVYPwPCm%2Fimage.png?alt=media&#x26;token=f1c4a85d-0778-4069-8719-2b129aa10fa5" alt=""><figcaption><p>VMWare Settings</p></figcaption></figure>

and add your network

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2F5UJRxlKSbKthK9VyjvtG%2Fimage.png?alt=media&#x26;token=043f7c82-d225-4498-9d12-e29663c8e87c" alt=""><figcaption><p>Adding a Network Adapter</p></figcaption></figure>

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2FEIxL9efdqPFRWM9LUxcj%2Fimage.png?alt=media&#x26;token=00d9f72c-714c-4a48-b124-e24799b522f5" alt=""><figcaption><p>Selecting our custom network adapter</p></figcaption></figure>

### GitLab Installation

{% hint style="info" %}
**Section Objective**

* Setting up a GitLab instance
* Setting up runners to compile projects
  {% endhint %}

{% hint style="warning" %}
If you don't see an IP Address, then try running `sudo dhclient <INTERFACE>`
{% endhint %}

Before starting this section, have a Linux server up and ready. The script below will install GitLab, Docker, and some quality-of-life tools such as Vim and ohmyzsh (you can remove these from the script if you do not want them).

Download the script then run the following commands:

```bash
chmod +x install_gitlab.sh
sudo ./install_gitlab.sh <IP ADDRESS> # Don't set it as localhost/127.0.0.1
```

{% @github-files/github-code-block url="<https://github.com/Securescape/Offensive-Development/blob/main/Install%20Scripts/install_gitlab.sh>" %}

Once the installation is complete, you should be able to open the GitLab web page from your host/Window VM at the selected IP Address

GitLab creates a random root password which you can get by running the command below

{% code overflow="wrap" %}

```bash
sudo cat /etc/gitlab/initial_root_password
```

{% endcode %}

Use this to log in to the web console

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2FeKON2VYRvIQl2QixB17x%2Fimage.png?alt=media&#x26;token=67fdd66d-e222-426b-be25-66781d65b079" alt=""><figcaption><p>Logging into our GitLab webpage</p></figcaption></figure>

{% hint style="danger" %}
Make sure to save or change the root password, as it will be deleted from the server files after logging in.
{% endhint %}

#### OPTIONAL - Adding an SSL Certificate

If you are implementing this in an organisation environment, setting up a TLS certificate is advisable to encrypt the traffic going to and from the server. We will not go through this in this blog, but you can easily find it on the GitLab documentation below.

{% embed url="<https://docs.gitlab.com/omnibus/settings/ssl/#enable-the-lets-encrypt-integration>" %}
GitLab SSL Installation for Linux
{% endembed %}

### Adding a Runner

{% hint style="info" %}
**Section Objectives**

* Create a Runner instance
* Register the runner to the GitLab server
  {% endhint %}

A runner is a program that runs on your server. It compiles and builds projects depending on the language it's using. We will need to create a runner for each language we use in our project, though multiple projects can use the same runner making it handy for multiple build pipelines.

Below are a couple of languages that you can have a runner use, there are a lot more though.

{% hint style="info" %}
**Runner Languages**

* Golang: <https://hub.docker.com/_/golang>
* Python: <https://hub.docker.com/_/python>
* C#: <https://hub.docker.com/_/microsoft-dotnet-sdk/>
* C: <https://hub.docker.com/_/gcc>
* Rust: <https://hub.docker.com/_/rust>

...
{% endhint %}

#### Installing a runner

To install a runner, we first need to get the dependencies, we've made this simpler by providing the script below:

{% @github-files/github-code-block url="<https://github.com/Securescape/Offensive-Development/blob/main/Install%20Scripts/install_runner.sh>" %}

Download it then execute it. After installation is complete, you will be met with a prompt - keep this open as we move on to the next step.

#### Getting a Token

Each runner we create requires a unique token which we can generate from the Runners tab, under CI/CD.

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2FG9YdQX3AYTU8wDUrflMP%2Fimage.png?alt=media&#x26;token=27c8136b-bd61-428e-8f0d-400387b3d675" alt=""><figcaption><p>CI/CD Runners Page</p></figcaption></figure>

Click on **New Instance Runner -> Linux -> Create runner** to open the runner config settings

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2Fo7O8ngeqLwOrrgq8R3yk%2Fimage.png?alt=media&#x26;token=70f780b8-cf87-4e34-84c5-00fe0cf4c3dc" alt=""><figcaption><p>Runner config</p></figcaption></figure>

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2FhSfbHcIRQwFxr1qh7o1M%2Fimage.png?alt=media&#x26;token=b6fce061-0a5b-4674-98d3-3adff38ae3fc" alt=""><figcaption><p>Runner token</p></figcaption></figure>

{% code title="Example Prompt" overflow="wrap" %}

```bash
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://192.168.8.142

Enter the registration token:
glrt-5yE22sixYMxP5jW4FLpq
Verifying runner... is valid    
                    runner=5yE22sixY
Enter a name for the runner. This is stored only in the local config.toml file:
[devops]: golang2

Enter an executor: custom, parallels, kubernetes, docker-autoscaler, instance, shell, ssh, virtualbox, docker, docker-windows, docker+machine:
docker

Enter the default Docker image (for example, ruby:2.7):
golang:1.22

Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
```

{% endcode %}

Copy the token, then go back to your terminal. Enter your GitLab server IP, then your runner's token, name your runner (you can give it the same name as the UI one), type `docker` for the executor, and then the language of your choice. Since I'll be using Go for this demo, use `golang:1.22`&#x20;

Once you are done, type in `sudo gitlab-runner` run to generate a callback to the server. This will register it for later use.

{% hint style="danger" %}
**Make sure to run the command as sudo**

`sudo gitlab-runner ...`
{% endhint %}

<details>

<summary>TL;DR Steps</summary>

1. Open the Runner screen from **Dashboard -> Build -> Runner -> New Instance Runner**
2. Create a Linux runner, add tags *(optional)*
3. Copy Runner Token
4. In your terminal, type `sudo gitlab-runner register`
5. Enter your GitLab IP address
6. Enter your runner's token
7. Enter a name for your runner
8. Enter `docker` for the executor
9. Enter a language you want the runner to compile
10. Execute `sudo gitlab-runner run`

</details>

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2FL1kkUXWnDqdAlrSQaS6V%2Fimage.png?alt=media&#x26;token=cd2f359f-303f-45ee-915e-03812a0739d0" alt=""><figcaption><p>Created Runners</p></figcaption></figure>

### Adding Users

{% hint style="info" %}
**Section Objectives**

* Create normal users
* Add an SSH key for the user
  {% endhint %}

Now that we have the core settings set up, we can start creating users by going to **Admin Area -> Users -> New User**

Fill in the required details, set the access level as **Regular**, then click **Create User**

Before logging out of root, click the Edit button next to the user and give them a username and password. You can optionally set up SMTP to send a password reset link instead.

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2Fnj0GQExruM3bzmIsiOyS%2Fimage.png?alt=media&#x26;token=e6a43b03-1193-4107-9f32-99fe383e6172" alt=""><figcaption><p>Editting the user</p></figcaption></figure>

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2FhrfIAFJAMjfoajhD7sy2%2Fimage.png?alt=media&#x26;token=b5b9333b-0cb1-48c4-958d-da838e120cb1" alt=""><figcaption><p>Password Change Section</p></figcaption></figure>

{% embed url="<https://docs.gitlab.com/omnibus/settings/smtp.html>" %}
GitLab SMTP Setup
{% endembed %}

Once the password has been set, log in to your user, go to **Edit Profile -> SSH Keys** and add a new SSH key. This will let you push and pull private repositories from this user. We will also do this for our Windows VM to connect our private GitLab repositories to TeamCity.

You can create one by running `ssh-keygen` if you do not have a copy or want a separate key for the server. SSH keys can be found in `/home/$USER/.ssh/SSH_KEY_NAME.pub`

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2FFEvMzn5qw1NMv3tumvlS%2Fimage.png?alt=media&#x26;token=b311fed3-0f2f-4085-96b1-e882107488a1" alt=""><figcaption><p>Adding an SSH Key</p></figcaption></figure>
