Skip to content

Commit f5b0cea

Browse files
committed
cr fixes
1 parent 1721880 commit f5b0cea

8 files changed

Lines changed: 77 additions & 210 deletions

File tree

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ifeq ($(filter run,$(MAKECMDGOALS)),run)
3030
. venv-jit/bin/activate && \
3131
export PYTHONPATH=$(CURDIR) && \
3232
python3 src/utils/github_topics_to_json_file.py && \
33-
python3 src/scripts/create_teams.py teams.json
33+
python3 src/scripts/sync_teams/sync_teams.py teams.json
3434
endif
3535

3636
install:
@@ -41,8 +41,8 @@ run:
4141
@echo run complete
4242

4343

44-
SELF_HOSTED_DOCKER_CENTOS_SCRIPT := src/scripts/self-hosted-runners/setup-self-hosted-runner-centos.sh
45-
SELF_HOSTED_DOCKER_UBUNTU_SCRIPT := src/scripts/self-hosted-runners/setup-self-hosted-runner-ubuntu.sh
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
4646
SELF_HOSTED_RUNNER_SCRIPT := src/scripts/self-hosted-runners/install-github-runner-agent.sh
4747

4848

README.md

Lines changed: 28 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,43 @@
22

33
The README provided is an extensive guide on how to use the `jit-customer-scripts` project. \
44
The Makefile described is composed of commands that ease the installation, configuration, and running of the scripts
5-
within the project. \
6-
Here's a breakdown of the provided README and Makefile:
5+
within the project.
6+
7+
## Prerequisites
78

8-
## README Overview
9+
- Python 3.x
10+
- Git
11+
- make
912

10-
- **Introduction**: The README begins with a brief description of the project and its purpose, which is to aid customers
11-
in creating teams and updating assets based on a given JSON file.
12-
- **Project Structure**: An organized visual representation of the project's file structure is provided.
13-
- **Prerequisites**: Lists necessary software that should be installed before starting, along with instructions for
14-
CentOS and Ubuntu to prepare the environment.
15-
- **Generating API Keys**: Guidance on how to generate the necessary Github Personal Access Token and Jit API Key.
16-
- **Installation**: Steps on cloning the repository and setting up the project.
17-
- **Configuration**: Instructions on how to configure the necessary environment variables for the script.
18-
- **Usage**:
19-
- **Creating Teams from Github Topics**: Details on how to use the script to create teams and update assets from
20-
GitHub topics.
21-
- **Using External JSON File**: Allows users to directly provide a JSON file containing team details.
22-
- **Excluding Topics**: Instructions on excluding specific topics when creating teams.
23-
- **Setting Up Self-Hosted Runners**: Guidance on setting up GitHub self-hosted runners on both CentOS and Ubuntu.
24-
- **Help**: Instruction on getting help with the Makefile commands.
25-
- **Development**: A small note about overriding the Jit API endpoint.
13+
To make sure you have all you can run this command:
2614

27-
## Makefile Overview
15+
### Centos distro
2816

29-
The Makefile consists of several targets to facilitate the installation, configuration, and running of scripts. Here's a
30-
breakdown:
17+
```shell
18+
sudo yum install -y git make && git clone https://github.com/jitsecurity/jit-customer-scripts.git && cd jit-customer-scripts
19+
```
3120

32-
### sync-teams
21+
### Ubuntu distro
3322

34-
This target has three sub-targets:
23+
```shell
24+
sudo apt install -y git make && git clone https://github.com/jitsecurity/jit-customer-scripts.git && cd jit-customer-scripts
25+
```
3526

36-
- `install`: Ensures Python 3 is installed, sets up a virtual environment, and installs the required dependencies.
37-
- `configure`: Prompts the user to input configuration details like GitHub organization name, API client ID, client
38-
secret, and GitHub token. The responses are written to a `.env` file.
39-
- `run`: Activates the virtual environment and runs two Python scripts in succession to generate teams.
27+
### Mac
4028

41-
### self-hosted-runner
29+
```shell
30+
brew install git make && git clone https://github.com/jitsecurity/jit-customer-scripts.git && cd jit-customer-scripts
31+
```
4232

43-
This target and its sub-targets facilitate setting up a GitHub self-hosted runner:
33+
Here's a breakdown of the provided README and Makefile:
4434

45-
- `check-root`: Ensures the script isn't run as root.
46-
- `setup-runner`: Checks the platform (CentOS or Ubuntu) and runs the appropriate setup script.
47-
- `install-agent`: Installs the GitHub runner agent.
35+
## Supported Scripts
4836

49-
### Help
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:
5039

51-
- `help`: Displays available targets and their brief descriptions.
40+
* [syncing jit teams](src/scripts/sync_teams/sync-teams.md)
41+
* [self-hosted-runners](src/scripts/self-hosted-runners/README.md)
5242

5343
## Hierarchical Structure of the Commands:
5444

@@ -71,7 +61,8 @@ jit-customer-scripts/
7161
│ └── scripts/
7262
│ └── create_teams.py
7363
| └── self-hosted-runners
74-
│ └── setup-self-hosted-runner-centos.sh
64+
│ └── setup-rootless-docker-centos.sh
65+
│ └── setup-rootless-docker-ubuntu.sh
7566
│ └── ...
7667
├── src/
7768
│ └── utils/
@@ -90,26 +81,6 @@ jit-customer-scripts/
9081
- `Makefile`: Provides commands to help with project setup and execution.
9182
- `README.md`: This file.
9283

93-
## Prerequisites
94-
95-
- Python 3.x
96-
- Git
97-
- make
98-
99-
To make sure you have all you can run this command:
100-
101-
#### Centos
102-
103-
```shell
104-
sudo yum install -y git make && git clone https://github.com/jitsecurity/jit-customer-scripts.git && cd jit-customer-scripts
105-
```
106-
107-
#### Ubuntu
108-
109-
```shell
110-
sudo apt install -y git make && git clone https://github.com/jitsecurity/jit-customer-scripts.git && cd jit-customer-scripts
111-
```
112-
11384
## Generating API Keys
11485

11586
* To generate Github Personal Access Token(PAT) refer to
@@ -119,142 +90,11 @@ sudo apt install -y git make && git clone https://github.com/jitsecurity/jit-cus
11990

12091
> We recommend generating a fine-grained PAT with read-only access to the organization for the use of this script.
12192
122-
## Installation
123-
124-
1. Clone the repository:
125-
126-
```shell
127-
git clone --branch 1.0.0 git@github.com:jitsecurity/jit-customer-scripts.git
128-
```
129-
130-
2. Change into the project directory:
131-
132-
```shell
133-
cd jit-customer-scripts
134-
```
135-
136-
## Sync Teams
137-
138-
### Creating Teams from Github Topics
139-
140-
To run the script and create teams and update assets, use the following command:
141-
142-
```shell
143-
make create-teams
144-
```
145-
146-
This command is a convenience utility that extracts the teams to generate from Github topics. \
147-
It runs these commands:
148-
149-
```bash
150-
python src/utils/github_topics_to_json_file.py
151-
python src/scripts/create_teams.py teams.json
152-
```
153-
154-
This command will fetch the repository names and topics from the GitHub API and generate the JSON file. And then it will
155-
create the teams and update the assets.
156-
157-
> We recommend using something like Github Actions and Github secrets to run this script on a schedule to make sure you
158-
> are always synced.
159-
160-
#### Using External JSON File
161-
162-
You can also provide a JSON file containing team details using a command line argument directly. The JSON file should
163-
have the following structure:
164-
165-
```json
166-
{
167-
"teams": [
168-
{
169-
"name": "Team 1",
170-
"members": [
171-
"user1",
172-
"user2"
173-
],
174-
"resources": [
175-
{
176-
"type": "{resource_type}",
177-
"name": "Resource 1"
178-
},
179-
{
180-
"type": "{resource_type}",
181-
"name": "Resource 2"
182-
}
183-
]
184-
},
185-
{
186-
"name": "Team 2",
187-
"members": [
188-
"user3",
189-
"user4"
190-
],
191-
"resources": [
192-
{
193-
"type": "{resource_type}",
194-
"name": "Resource 3"
195-
}
196-
]
197-
}
198-
]
199-
}
200-
```
201-
202-
You can run the command like this:
203-
204-
```shell
205-
python scripts/create_teams.py path/to/teams.json
206-
```
207-
208-
Replace `path/to/teams.json` with the actual path to your JSON file.
209-
210-
#### Excluding Topics
211-
212-
You can exclude certain topics from being considered when creating teams. \
213-
To exclude topics, you could add them in the `make configure` command or update this env var in
214-
the `.env` file: `TEAM_WILDCARD_TO_EXCLUDE`.
215-
216-
For example, to exclude topics that contain the word "test", you can set the variable as follows:
217-
218-
TEAM_WILDCARD_TO_EXCLUDE=*test*
219-
220-
This will exclude topics with names like "test", "test123", and "abc-testing".
221-
222-
#### Development
93+
## Development
22394

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

227-
## Settings Up Self-Hosted Runners
228-
229-
To setup self-hosted runners, use the following command:
230-
231-
You need to take the self hosted runners token from the Github Actions page of your repository.
232-
`https://github.com/<your-github-org-name>/jit/settings/actions/runners`
233-
234-
### Running on CentOS
235-
236-
```shell
237-
make self-hosted-runner centos runner_token=<runner-token> github_org=<github-organization> jit_repo=<jit-repo-name>
238-
```
239-
240-
You will be prompted to answer some questions about your runner. \
241-
When you complete this step, restart your EC2 machine. \
242-
The runner will be automatically started on boot.
243-
244-
Replace `<runner-token>`, `<github-organization>`, and `<jit-repo-name>` with the appropriate values.
245-
246-
### Running on Ubuntu
247-
248-
```shell
249-
make self-hosted-runner ubuntu runner_token=<runner-token> github_org=<github-organization>
250-
```
251-
252-
You will be prompted to answer some questions about your runner. \
253-
When you complete this step, restart your EC2 machine. \
254-
The runner will be automatically started on boot.
255-
256-
Replace `<runner-token>` and `<github-organization>` with the appropriate values.
257-
25898
## Help
25999

260100
To see the available commands and their descriptions, use the following command:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Settings Up Self-Hosted Runners
2+
3+
This Command and its sub-targets facilitate setting up a GitHub self-hosted runner on different OS:
4+
5+
- `centos`: Setup self hosted runner on CentOS.
6+
- `ubuntu`: Setup self hosted runner on Ubuntu.
7+
8+
You need to take the self hosted runners token from the Github Actions page of your repository.
9+
`https://github.com/<your-github-org-name>/jit/settings/actions/runners`
10+
11+
## Running on CentOS
12+
13+
```shell
14+
make self-hosted-runner centos runner_token=<runner-token> github_org=<github-organization>
15+
```
16+
17+
You will be prompted to answer some questions about your runner. \
18+
When you complete this step, restart your EC2 machine. \
19+
The runner will be automatically started on boot.
20+
21+
Replace `<runner-token>` and `<github-organization>` with the appropriate values.
22+
23+
## Running on Ubuntu
24+
25+
```shell
26+
make self-hosted-runner ubuntu runner_token=<runner-token> github_org=<github-organization>
27+
```
28+
29+
You will be prompted to answer some questions about your runner. \
30+
When you complete this step, restart your EC2 machine. \
31+
The runner will be automatically started on boot.
32+
33+
Replace `<runner-token>` and `<github-organization>` with the appropriate values.

src/scripts/self-hosted-runners/setup-self-hosted-runner-centos.sh renamed to src/scripts/self-hosted-runners/setup-rootless-docker-centos.sh

File renamed without changes.

src/scripts/self-hosted-runners/setup-self-hosted-runner-ubuntu.sh renamed to src/scripts/self-hosted-runners/setup-rootless-docker-ubuntu.sh

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,18 @@
33
# Exit on error
44
set -e
55

6-
# Check if script is run as root
7-
if [ "$EUID" -eq 0 ]; then
8-
echo "Please run this script as a non-root user."
9-
exit 1
10-
fi
11-
126
# Install required packages only if they aren't already installed
137
sudo apt update
148
# Check for curl
159
if ! command -v curl &> /dev/null; then
16-
echo "Installing curl..."
17-
sudo apt install -y curl
10+
echo "Installing curl..."
11+
sudo apt install -y curl
1812
fi
1913

2014
# Check for iptables
2115
if ! command -v iptables &> /dev/null; then
22-
echo "Installing iptables..."
23-
sudo apt install -y iptables
16+
echo "Installing iptables..."
17+
sudo apt install -y iptables
2418
fi
2519

2620
sudo apt install -y uidmap

src/scripts/sync_teams/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import pytest
66
from faker import Faker
7-
from src.scripts.create_teams import parse_input_file, update_assets
8-
from src.scripts.create_teams import process_teams
7+
from src.scripts.sync_teams.sync_teams import parse_input_file, update_assets
8+
from src.scripts.sync_teams.sync_teams import process_teams
99
from src.shared.models import Organization
1010
from tests.factories import AssetFactory, TeamAttributesFactory, TeamStructureFactory
1111
from tests.factories import OrganizationFactory
@@ -36,7 +36,7 @@ def organization():
3636
def test_parse_input_file(json_data, expected_teams):
3737
with open("test_input.json", "w") as file:
3838
file.write(json_data)
39-
with patch("src.scripts.create_teams.argparse.ArgumentParser.parse_args") as mock_parse_args:
39+
with patch("src.scripts.sync_teams.sync_teams.argparse.ArgumentParser.parse_args") as mock_parse_args:
4040
mock_parse_args.return_value.file = "test_input.json"
4141
result = parse_input_file()
4242
assert len(result.teams) == expected_teams
@@ -65,7 +65,7 @@ def test_parse_input_file__with_invalid_json(invalid_file, json_data, should_rai
6565
if invalid_file:
6666
with open(invalid_file, "w") as file:
6767
file.write(json_data)
68-
with patch("src.scripts.create_teams.argparse.ArgumentParser.parse_args") as mock_parse_args:
68+
with patch("src.scripts.sync_teams.sync_teams.argparse.ArgumentParser.parse_args") as mock_parse_args:
6969
mock_parse_args.return_value.file = invalid_file
7070
if should_raise:
7171
with pytest.raises(expected_exception) as exc_info:
@@ -108,8 +108,8 @@ def test_process_teams(label, existing_teams_indexes, asset_indexes, data, len_e
108108
assets = [assets[i] for i in
109109
asset_indexes] if asset_indexes != "all" else assets
110110

111-
with patch("src.scripts.create_teams.get_existing_teams") as mock_get_existing_teams:
112-
with patch("src.scripts.create_teams.create_teams") as mock_create_teams:
111+
with patch("src.scripts.sync_teams.sync_teams.get_existing_teams") as mock_get_existing_teams:
112+
with patch("src.scripts.sync_teams.sync_teams.create_teams") as mock_create_teams:
113113
mock_get_existing_teams.return_value = existing_teams
114114
teams_to_delete = process_teams("token", organization, assets)
115115
assert len(teams_to_delete) == len_expected_teams_to_delete
@@ -118,8 +118,8 @@ def test_process_teams(label, existing_teams_indexes, asset_indexes, data, len_e
118118
def test_update_assets(data):
119119
# Test with no assets
120120
organization, assets, teams = data
121-
# with patch("src.scripts.create_teams.list_assets") as mock_list_assets:
121+
# with patch("src.scripts.sync_teams.sync_teams.list_assets") as mock_list_assets:
122122
# mock_list_assets.return_value = []
123-
with patch("src.scripts.create_teams.add_teams_to_asset") as mock_add_teams_to_asset:
123+
with patch("src.scripts.sync_teams.sync_teams.add_teams_to_asset") as mock_add_teams_to_asset:
124124
update_assets("token", assets, organization)
125125
assert mock_add_teams_to_asset.call_count == 10

0 commit comments

Comments
 (0)