Skip to content

Commit 9191d12

Browse files
authored
Merge pull request #13 from jitsecurity/sc-20043-self-hosted-runners-customer-scripts-part2
Sc 20043 self hosted runners customer scripts part2
2 parents 3432737 + 53c6bb1 commit 9191d12

11 files changed

Lines changed: 315 additions & 192 deletions

File tree

Makefile

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
SHELL := /bin/bash
22

3-
install:
3+
.PHONY: sync-teams self-hosted-runner setup-runner install-agent centos ubuntu help
4+
5+
6+
sync-teams:
7+
ifeq ($(filter install,$(MAKECMDGOALS)),install)
48
@if ! command -v python3 >/dev/null 2>&1; then \
59
echo "Python 3 is required but it's not installed. Please install Python 3 (or ensure 'python3' command is available) and try again." >&2; \
610
exit 1; \
@@ -9,8 +13,8 @@ install:
913
python3 -m venv venv-jit; \
1014
fi
1115
. venv-jit/bin/activate && pip install -r requirements.txt
12-
13-
configure:
16+
endif
17+
ifeq ($(filter configure,$(MAKECMDGOALS)),configure)
1418
@read -p "Enter GitHub organization name: " org_name; \
1519
read -p "Enter JIT API client ID: " client_id; \
1620
read -p "Enter JIT API client secret: " client_secret; \
@@ -21,20 +25,54 @@ configure:
2125
echo "JIT_CLIENT_SECRET=$$client_secret" >> .env; \
2226
echo "GITHUB_API_TOKEN=$$github_token" >> .env; \
2327
echo "TEAM_WILDCARD_TO_EXCLUDE=$$topics_to_exclude" >> .env
24-
25-
create-teams:
28+
endif
29+
ifeq ($(filter run,$(MAKECMDGOALS)),run)
2630
. venv-jit/bin/activate && \
2731
export PYTHONPATH=$(CURDIR) && \
28-
python3 src/utils/github_topics_to_json_file.py && \
29-
python3 src/scripts/create_teams.py teams.json
32+
python3 src/utils/github_topics_to_json_file.py && \
33+
python3 src/scripts/sync_teams/sync_teams.py teams.json
34+
endif
35+
36+
install:
37+
@echo installation complete
38+
configure:
39+
@echo configuration complete
40+
run:
41+
@echo run complete
42+
43+
44+
SELF_HOSTED_DOCKER_CENTOS_SCRIPT := src/scripts/self-hosted-runners/setup-rootless-docker-centos.sh
45+
SELF_HOSTED_DOCKER_UBUNTU_SCRIPT := src/scripts/self-hosted-runners/setup-rootless-docker-ubuntu.sh
46+
SELF_HOSTED_RUNNER_SCRIPT := src/scripts/self-hosted-runners/install-github-runner-agent.sh
47+
48+
49+
self-hosted-runner: check-root setup-runner install-agent
50+
51+
check-root:
52+
@if [ "$$UID" -eq 0 ]; then \
53+
echo "Error: This script should not be run as root."; \
54+
exit 1; \
55+
fi
56+
57+
setup-runner:
58+
ifeq ($(filter centos,$(MAKECMDGOALS)),centos)
59+
sudo yum install -y jq
60+
chmod +x $(SELF_HOSTED_DOCKER_CENTOS_SCRIPT)
61+
./$(SELF_HOSTED_DOCKER_CENTOS_SCRIPT)
62+
else ifeq ($(filter ubuntu,$(MAKECMDGOALS)),ubuntu)
63+
chmod +x $(SELF_HOSTED_DOCKER_UBUNTU_SCRIPT)
64+
./$(SELF_HOSTED_DOCKER_UBUNTU_SCRIPT)
65+
endif
66+
67+
install-agent:
68+
chmod +x $(SELF_HOSTED_RUNNER_SCRIPT)
69+
./$(SELF_HOSTED_RUNNER_SCRIPT) $(runner_token) $(github_org)
3070

31-
setup-self-hosted-runner-centos:
32-
sudo yum install -y jq && \
33-
chmod +x src/scripts/self-hosted-runners/setup-self-hosted-runner-centos.sh && \
34-
./src/scripts/self-hosted-runners/setup-self-hosted-runner-centos.sh && \
35-
chmod +x src/scripts/self-hosted-runners/install-github-runner-agent.sh && \
36-
./src/scripts/self-hosted-runners/install-github-runner-agent.sh $(token) $(github_organization)
71+
centos:
72+
@echo installed on centos
3773

74+
ubuntu:
75+
@echo installed on ubuntu
3876

3977
help:
4078
@echo "Usage: make [target]"
@@ -43,4 +81,7 @@ help:
4381
@echo " install Install dependencies"
4482
@echo " configure Configure environment variables"
4583
@echo " create-teams Create teams based on input file"
84+
@echo " self-hosted-runner centos runner_token=<runner-token> github_org=<github-organization> Set up self-hosted runner on CentOS"
85+
@echo " self-hosted-runner ubuntu runner_token=<runner-token> github_org=<github-organization> Set up self-hosted runner on Ubuntu"
86+
@echo " install-agent Install GitHub runner agent"
4687
@echo " help Show this help message"

README.md

Lines changed: 58 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
1-
# Jit Customer Scripts
1+
# Jit Customer Scripts README
22

3-
[![codecov](https://codecov.io/gh/jitsecurity/jit-customer-scripts/graph/badge.svg?token=76IhFwTPjv)](https://codecov.io/gh/jitsecurity/jit-customer-scripts)
3+
The README provided is an extensive guide on how to use the `jit-customer-scripts` project. \
4+
The Makefile described is composed of commands that ease the installation, configuration, and running of the scripts
5+
within the project.
46

5-
This project provides customer scripts to help them with their JIT solution. \
6-
The `create-teams.py` script's goal is to create teams and update assets based on the provided JSON file.
7+
## Prerequisites
8+
9+
- Python 3.x
10+
- Git
11+
- make
12+
13+
To make sure you have all you can run this command:
14+
15+
### Centos distro
16+
17+
```shell
18+
sudo yum install -y git make && git clone https://github.com/jitsecurity/jit-customer-scripts.git && cd jit-customer-scripts
19+
```
20+
21+
### Ubuntu distro
22+
23+
```shell
24+
sudo apt update && sudo apt install -y git make && git clone https://github.com/jitsecurity/jit-customer-scripts.git && cd jit-customer-scripts
25+
```
26+
27+
### Mac
28+
29+
```shell
30+
brew install git make && git clone https://github.com/jitsecurity/jit-customer-scripts.git && cd jit-customer-scripts
31+
```
32+
33+
Here's a breakdown of the provided README and Makefile:
34+
35+
## Supported Scripts
36+
37+
The Makefile consists of several commands to facilitate the installation, configuration, and running of scripts. Here's
38+
a link to the docs for each one:
39+
40+
* [syncing jit teams](src/scripts/sync_teams/sync-teams.md)
41+
* [self-hosted-runners](src/scripts/self-hosted-runners/self-hosted-runner.md)
42+
43+
## Hierarchical Structure of the Commands:
44+
45+
1. **sync-teams**
46+
- install
47+
- configure
48+
- run
49+
50+
2. **self-hosted-runner**
51+
- centos
52+
- ubuntu
753

854
## Project Structure
955

@@ -13,9 +59,11 @@ The project has the following structure:
1359
jit-customer-scripts/
1460
├── src/
1561
│ └── scripts/
16-
│ └── create_teams.py
62+
│ └── sync_teams
63+
│ └── sync_teams.py
1764
| └── self-hosted-runners
18-
│ └── setup-self-hosted-runner-centos.sh
65+
│ └── setup-rootless-docker-centos.sh
66+
│ └── setup-rootless-docker-ubuntu.sh
1967
│ └── ...
2068
├── src/
2169
│ └── utils/
@@ -34,26 +82,6 @@ jit-customer-scripts/
3482
- `Makefile`: Provides commands to help with project setup and execution.
3583
- `README.md`: This file.
3684

37-
## Prerequisites
38-
39-
- Python 3.x
40-
- Git
41-
- make
42-
43-
To make sure you have all you can run this command:
44-
45-
#### Centos
46-
47-
```shell
48-
sudo yum install -y git make && git clone https://github.com/jitsecurity/jit-customer-scripts.git && cd jit-customer-scripts
49-
```
50-
51-
#### Ubuntu
52-
53-
```shell
54-
sudo apt install -y git make && git clone https://github.com/jitsecurity/jit-customer-scripts.git && cd jit-customer-scripts
55-
```
56-
5785
## Generating API Keys
5886

5987
* To generate Github Personal Access Token(PAT) refer to
@@ -63,148 +91,15 @@ sudo apt install -y git make && git clone https://github.com/jitsecurity/jit-cus
6391

6492
> We recommend generating a fine-grained PAT with read-only access to the organization for the use of this script.
6593
66-
## Installation
67-
68-
1. Clone the repository:
69-
70-
```shell
71-
git clone --branch 1.0.0 git@github.com:jitsecurity/jit-customer-scripts.git
72-
```
73-
74-
2. Change into the project directory:
75-
76-
```shell
77-
cd jit-customer-scripts
78-
```
79-
80-
3. Create a virtual environment and install the required dependencies:
81-
82-
```shell
83-
make install
84-
```
85-
86-
## Configuration
87-
88-
Before running the script, you need to configure the necessary environment variables. Follow these steps:
89-
90-
1. Run the configuration command:
91-
92-
```shell
93-
make configure
94-
```
95-
96-
2. Enter the required information when prompted:
97-
- GitHub organization name
98-
- API client ID
99-
- API client secret
100-
- GitHub token
101-
102-
3. The command will generate a `.env` file with the provided information.
103-
104-
## Usage
105-
106-
### Creating Teams from Github Topics
107-
108-
To run the script and create teams and update assets, use the following command:
109-
110-
```shell
111-
make create-teams
112-
```
113-
114-
This command is a convenience utility that extracts the teams to generate from Github topics. \
115-
It runs these commands:
116-
117-
```bash
118-
python src/utils/github_topics_to_json_file.py
119-
python src/scripts/create_teams.py teams.json
120-
```
121-
122-
This command will fetch the repository names and topics from the GitHub API and generate the JSON file. And then it will
123-
create the teams and update the assets.
124-
125-
> We recommend using something like Github Actions and Github secrets to run this script on a schedule to make sure you
126-
> are always synced.
127-
128-
#### Using External JSON File
129-
130-
You can also provide a JSON file containing team details using a command line argument directly. The JSON file should
131-
have the following structure:
132-
133-
```json
134-
{
135-
"teams": [
136-
{
137-
"name": "Team 1",
138-
"members": [
139-
"user1",
140-
"user2"
141-
],
142-
"resources": [
143-
{
144-
"type": "{resource_type}",
145-
"name": "Resource 1"
146-
},
147-
{
148-
"type": "{resource_type}",
149-
"name": "Resource 2"
150-
}
151-
]
152-
},
153-
{
154-
"name": "Team 2",
155-
"members": [
156-
"user3",
157-
"user4"
158-
],
159-
"resources": [
160-
{
161-
"type": "{resource_type}",
162-
"name": "Resource 3"
163-
}
164-
]
165-
}
166-
]
167-
}
168-
```
169-
170-
You can run the command like this:
171-
172-
```shell
173-
python scripts/create_teams.py path/to/teams.json
174-
```
175-
176-
Replace `path/to/teams.json` with the actual path to your JSON file.
177-
178-
#### Excluding Topics
179-
180-
You can exclude certain topics from being considered when creating teams. \
181-
To exclude topics, you could add them in the `make configure` command or update this env var in
182-
the `.env` file: `TEAM_WILDCARD_TO_EXCLUDE`.
183-
184-
For example, to exclude topics that contain the word "test", you can set the variable as follows:
185-
186-
TEAM_WILDCARD_TO_EXCLUDE=*test*
187-
188-
This will exclude topics with names like "test", "test123", and "abc-testing".
189-
190-
#### Development
94+
## Development
19195

19296
To override Jit's API endpoint, you can set the `JIT_API_ENDPOINT` environment variable. If the variable is not set, the
19397
default value will be used.
19498

195-
### Settings Up Self-Hosted Runners
196-
197-
To setup self-hosted runners, use the following command:
99+
## Help
198100

199-
You need to take the self hosted runners token from the Github Actions page of your repository.
200-
`https://github.com/<your-github-org-name>/jit/settings/actions/runners`
201-
202-
#### Running on CentOS
101+
To see the available commands and their descriptions, use the following command:
203102

204103
```shell
205-
make setup-self-hosted-runner-centos token=<your-token> github_organization=<your-github-org-name>
104+
make help
206105
```
207-
208-
You will be prompted to answer some questions about your runner. \
209-
When you complete this step, restart your EC2 machine. \
210-
The runner will be automatically started on boot.

src/scripts/self-hosted-runners/install-github-runner-agent.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
set -e
55

66
# Assigning arguments to named variables for clarity
7-
user_token="$1"
7+
runner_token="$1"
88
github_organization="$2"
99

1010
# Ensure both arguments are provided
11-
if [ -z "$user_token" ] || [ -z "$github_organization" ]; then
12-
echo "Usage: $0 <user_token> <github_organization>"
13-
exit 1
11+
if [ -z "$runner_token" ] || [ -z "$github_organization" ]; then
12+
echo "Usage: $0 <runner_token> <github_organization>"
13+
exit 1
1414
fi
1515

16-
mkdir actions-runner && cd actions-runner
16+
mkdir ~/actions-runner && cd ~/actions-runner
1717
curl -o actions-runner-linux-x64-2.308.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.308.0/actions-runner-linux-x64-2.308.0.tar.gz
1818
tar xzf ./actions-runner-linux-x64-2.308.0.tar.gz
1919
# Create the runner and start the configuration experience
20-
./config.sh --url "https://github.com/$github_organization" --token "$user_token"
20+
./config.sh --url "https://github.com/$github_organization" --token "$runner_token"
2121

22-
sudo ./svc.sh install ec2-user
22+
sudo ./svc.sh install $USER

0 commit comments

Comments
 (0)