Skip to content

Commit b7ce93c

Browse files
authored
Adding working GitHub action workflow (#26)
* Fixed configs. Currently working locally. * Updated workflow. * Updated GitHub action workflow. * Removed daily cron workflow schedule.
1 parent 96f2537 commit b7ce93c

9 files changed

+113
-141
lines changed

.github/workflows/workflow.yml

+35-100
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,35 @@
1-
name: 'Apply latest configuration profiles via Fleet'
2-
description: 'Applies the latest MDM configuration profiles to a Fleet team'
3-
4-
inputs:
5-
FLEET_API_TOKEN:
6-
description: 'Fleet API Token'
7-
required: true
8-
FLEET_URL:
9-
description: 'Fleet URL'
10-
required: true
11-
12-
runs:
13-
using: 'composite'
14-
steps:
15-
- name: Checkout code
16-
uses: actions/checkout@v3
17-
with:
18-
repository: ${{ inputs.MDM_CONFIG_REPO }}
19-
20-
- name: Install fleetctl
21-
run: npm install -g fleetctl
22-
shell: bash
23-
24-
- name: Configure fleetctl
25-
run: fleetctl config set --address ${{ inputs.FLEET_URL }} --token ${{ inputs.FLEET_API_TOKEN }}
26-
shell: bash
27-
28-
- name: Run fleetctl apply
29-
run: |
30-
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
31-
# STEP 1: Apply config
32-
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33-
# Combine the contents of all the top-level config into a single YAML document
34-
35-
# - Include the contents of no-team.controls.yml
36-
# TODO
37-
38-
# - Include the contents of default.settings.yml
39-
# TODO
40-
41-
# - Include anything of `kind: config` in default.queries.yml
42-
# TODO
43-
44-
# - Include anything of `kind: config` in default.policies.yml
45-
# TODO
46-
47-
# Apply top-level config with `--replace` (replaces all existing top-level config)
48-
# TODO
49-
50-
# Loop through folders in /teams and combine the contents of all team config into a single YAML document
51-
52-
# - Include the contents of <folder-name>.controls.yml
53-
# TODO
54-
55-
# - Include the contents of <folder-name>.settings.yml
56-
# TODO
57-
58-
# - Include anything of `kind: team` in <folder-name>.queries.yml
59-
# TODO
60-
61-
# - Include anything of `kind: team` in <folder-name>.policies.yml
62-
# TODO
63-
64-
# Apply team config with `--replace` (replaces all existing config for this team)
65-
# TODO
66-
67-
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68-
# STEP 2: Apply queries
69-
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
70-
# Compile all queries into a single YAML document
71-
72-
# - Include anything of `kind: query` in default.queries.yml
73-
# TODO
74-
75-
# - Loop through folders in /teams and include anything of `kind: query` in <folder-name>.queries.yml for each
76-
# TODO
77-
78-
# Apply compiled queries YAML with `--replace` (deletes any queries in Fleet that aren't present in this config)
79-
# TODO
80-
81-
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82-
# STEP 3: Apply policies
83-
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
84-
# Compile all policies into a single YAML document
85-
# TODO
86-
87-
# - Include anything of `kind: policy` in default.policies.yml
88-
# TODO
89-
90-
# - Loop through folders in /teams and include anything of `kind: policy` in <folder-name>.policies.yml for each
91-
# TODO
92-
93-
# Apply compiled policies YAML with `--replace` (deletes any policies in Fleet that aren't present in this config)
94-
# TODO
95-
96-
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97-
# All done!
98-
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
99-
100-
shell: bash
1+
name: 'Apply latest configuration to Fleet'
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch: # allows manual triggering
8+
9+
defaults:
10+
run:
11+
shell: bash
12+
13+
# Add FLEET_URL and FLEET_API_TOKEN to the repository secrets.
14+
# In addition, specify or add secrets for all the environment variables that are mentioned in the global/team YAML files.
15+
env:
16+
FLEET_SSO_METADATA: ${{ secrets.FLEET_SSO_METADATA }}
17+
FLEET_GLOBAL_ENROLL_SECRET: ${{ secrets.FLEET_GLOBAL_ENROLL_SECRET }}
18+
FLEET_WORKSTATIONS_ENROLL_SECRET: ${{ secrets.FLEET_WORKSTATIONS_ENROLL_SECRET }}
19+
FLEET_WORKSTATIONS_CANARY_ENROLL_SECRET: ${{ secrets.FLEET_WORKSTATIONS_CANARY_ENROLL_SECRET }}
20+
21+
jobs:
22+
build-docker:
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Checkout GitOps repository
26+
uses: actions/checkout@v4
27+
28+
- name: Install fleetctl
29+
run: npm install -g fleetctl
30+
31+
- name: Configure fleetctl
32+
run: fleetctl config set --address ${{ secrets.FLEET_URL }} --token ${{ secrets.FLEET_API_TOKEN }}
33+
34+
- name: Run fleetctl gitops commands
35+
run: ./workflow.sh

README.md

+13-6
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@
22

33
> #### 🚧 Fleet GitOps is currently in development.
44
>
5-
> In the mean time, you can explore this repo for a preview of Fleet's GitOps best practice, and track its progress [here](https://github.com/fleetdm/fleet/issues/13643).
5+
> In the meantime, you can explore this repo for a preview of Fleet's GitOps best practice, and track its progress [here](https://github.com/fleetdm/fleet/issues/13643).
66
77
This folder illustrates the best practice for using Fleet with a GitOps workflow.
88

9-
How to setup a GitOps workflow to manage Fleet:
9+
How to set up a GitOps workflow to manage Fleet:
1010

1111
1. Clone this repository.
1212

1313
2. Make any changes to policies, queries, controls, and more defined in the files.
1414

15-
3. Create your own GitHub repository and push your cloned and modified code to your repo.
15+
3. Create your own GitHub repository.
1616

17-
4. Add Fleet's open-source [GitHub action](https://github.com/fleetdm/fleet-gitops) to your repo. Now, when anyone makes a change to any file, the action will run and update Fleet.
17+
4. Add `FLEET_URL` and `FLEET_API_KEY` to your GitHub repository secrets.
18+
19+
5. Update the `env` section of your [.github/workflows/workflow.yml](https://github.com/fleetdm/fleet-gitops/blob/main/.github/workflows/workflow.yml) file for all the environment variables that are mentioned in your `default.yml` and `teams/*.yml` YAML files. For the environment variables that map to secrets, add the required secrets to your GitHub repository secrets.
20+
21+
6. Push your cloned and modified code to your repo.
22+
23+
7. In GitHub, enable the `Apply latest configuration to Fleet` GitHub Actions workflow, and run workflow manually. Now, when anyone pushes a new commit, the action will run and update Fleet.
1824

1925
#### Structure:
2026

2127
- `lib/` - folder for policies, queries, configuration profiles, scripts, and agent options. These files can be referenced in top level keys in the `default.yml` file and the files in the `teams/` folder.
2228
- `default.yml` - file that defines the queries, policies, controls, and agent options for all hosts. If you're using Fleet Premium, this file updates queries and policies that run on all hosts ("All teams"). Controls and agent options are defined for hosts on "No team."
23-
- `teams/` - folder for teams in Fleet. These files define the controls, queries, policies, and agent options for hosts assigned to the specified team.
24-
- `.github/workflows/workflow.yml` - TODO: Explain how to add/update secrets. That's what this file is used for
29+
- `teams/` - folder for teams in Fleet. These `*.yml` files define the controls, queries, policies, and agent options for hosts assigned to the specified team.
30+
- `.github/workflows/workflow.yml` - the GitHub Actions workflow file that applies the latest configuration to Fleet.
31+
- `workflow.sh` - the bash script that applies the latest configuration to Fleet by executing `fleetctl gitops`. This script is used in the GitHub Actions workflow file. It can be run standalone during development.

default.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ controls: # Controls added to "No team"
66
macos_settings:
77
custom_settings:
88
- path: ./lib/macos-password.mobileconfig
9-
# DDM support coming soon
10-
# - path: ../lib/macos-os-updates.ddm.json
9+
# - path: ../lib/macos-os-updates.ddm.json (DDM coming soon)
1110
windows_settings:
1211
custom_settings:
13-
- path: ./lib/windows-screenlock.xml
12+
- path: ./lib/windows-screenlock.xml
1413
scripts:
1514
- path: ./lib/collect-fleetd-logs.sh
1615
queries:
17-
- path: ./lib/collect-fleetd-update-channels.queries.yml
16+
- path: ./lib/collect-fleetd-update-channels.queries.yml
1817
policies:
1918
agent_options:
2019
path: ./lib/agent-options.yml

lib/agent-options.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ command_line_flags:
22
config:
33
decorators:
44
load:
5-
- SELECT uuid AS host_uuid FROM system_info;
6-
- SELECT hostname AS hostname FROM system_info;
5+
- SELECT uuid AS host_uuid FROM system_info;
6+
- SELECT hostname AS hostname FROM system_info;
77
options:
88
disable_distributed: false
99
distributed_interval: 10

lib/collect-fleetd-update-channels.queries.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- name: Collect fleetd update channels
1+
- name: Collect fleetd update channels
22
description: "Collects the update channels for all fleetd components: osquery, Orbit, and Fleet Desktop. To see which version number each channel is on, ask in #help-engineering."
33
query: SELECT desktop_channel, orbit_channel, osqueryd_channel FROM orbit_info;
44
interval: 300 # 5 minutes

lib/collect-usb-devices.queries.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- name: Collect USB devices
1+
- name: Collect USB devices
22
description: Collects the USB devices that are currently connected to macOS and Linux hosts.
33
query: SELECT model, vendor FROM usb_devices;
44
interval: 300 # 5 minutes

teams/workstations-canary.yml

+18-18
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,32 @@ controls:
1515
grace_period_days: 2
1616
macos_settings:
1717
custom_settings:
18-
# - path: ../lib/macos-os-updates.ddm.json (DDM coming soon)
19-
- path: ../lib/macos-password.mobileconfig
18+
# - path: ../lib/macos-os-updates.ddm.json (DDM coming soon)
19+
- path: ../lib/macos-password.mobileconfig
2020
windows_settings:
2121
custom_settings:
22-
- path: ../lib/windows-screenlock.xml
22+
- path: ../lib/windows-screenlock.xml
2323
macos_setup:
24-
# bootstrap_package: https://github.com/organinzation/repository/bootstrap-package.pkg (Example URL)
25-
enable_end_user_authentication: true
26-
macos_setup_assistant: ../lib/automatic-enrollment.dep.json
24+
# bootstrap_package: https://github.com/organinzation/repository/bootstrap-package.pkg (Example URL)
25+
# enable_end_user_authentication: true
26+
macos_setup_assistant: ../lib/automatic-enrollment.dep.json
2727
scripts:
28-
- path: ../lib/remove-zoom-artifacts.script.sh
29-
- path: ../lib/set-timezone.script.sh
30-
queries:
31-
- path: ../lib/collect-usb-devices.queries.yml
32-
- path: ../lib/collect-failed-login-attempts.queries.yml
33-
- name: Collect fleetd update channels
34-
description: "Collects the update channels for all fleetd components: osquery, Orbit, and Fleet Desktop. To see which version number each channel is on, ask in #help-engineering."
35-
query: SELECT desktop_channel, orbit_channel, osqueryd_channel FROM orbit_info;
36-
interval: 300
37-
observer_can_run: true
38-
automations_enabled: false
28+
- path: ../lib/remove-zoom-artifacts.script.sh
29+
- path: ../lib/set-timezone.script.sh
30+
queries:
31+
- path: ../lib/collect-usb-devices.queries.yml
32+
- path: ../lib/collect-failed-login-attempts.queries.yml
33+
- name: Collect fleetd update channels
34+
description: "Collects the update channels for all fleetd components: osquery, Orbit, and Fleet Desktop. To see which version number each channel is on, ask in #help-engineering."
35+
query: SELECT desktop_channel, orbit_channel, osqueryd_channel FROM orbit_info;
36+
interval: 300
37+
observer_can_run: true
38+
automations_enabled: false
3939
policies:
4040
- path: ../lib/macos-device-health.policies.yml
4141
- path: ../lib/windows-device-health.policies.yml
4242
agent_options:
4343
path: ../lib/agent-options.yml
4444
team_settings:
4545
secrets:
46-
- secret: "$FLEET_WORKSTATIONS_CANARY_ENROLL_SECRET"
46+
- secret: "$FLEET_WORKSTATIONS_CANARY_ENROLL_SECRET"

teams/workstations.yml

+9-9
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,26 @@ controls:
1515
grace_period_days: 2
1616
macos_settings:
1717
custom_settings:
18-
# - path: ../lib/macos-os-updates.ddm.json (DDM coming soon)
19-
- path: ../lib/macos-password.mobileconfig
18+
# - path: ../lib/macos-os-updates.ddm.json (DDM coming soon)
19+
- path: ../lib/macos-password.mobileconfig
2020
windows_settings:
2121
custom_settings:
2222
- path: ../lib/windows-screenlock.xml
2323
macos_setup:
2424
# bootstrap_package: https://github.com/organinzation/repository/bootstrap-package.pkg (example URL)
25-
enable_end_user_authentication: true
25+
# enable_end_user_authentication: true
2626
macos_setup_assistant: ../lib/automatic-enrollment.dep.json
2727
scripts:
28-
- path: ../lib/remove-zoom-artifacts.script.sh
29-
- path: ../lib/set-timezone.script.sh
30-
queries:
31-
- path: ../lib/collect-usb-devices.queries.yml
32-
- path: ../lib/collect-failed-login-attempts.queries.yml
28+
- path: ../lib/remove-zoom-artifacts.script.sh
29+
- path: ../lib/set-timezone.script.sh
30+
queries:
31+
- path: ../lib/collect-usb-devices.queries.yml
32+
- path: ../lib/collect-failed-login-attempts.queries.yml
3333
policies:
3434
- path: ../lib/macos-device-health.policies.yml
3535
- path: ../lib/windows-device-health.policies.yml
3636
agent_options:
3737
path: ../lib/agent-options.yml
3838
team_settings:
3939
secrets:
40-
- secret: "$FLEET_WORKSTATIONS_ENROLL_SECRET"
40+
- secret: "$FLEET_WORKSTATIONS_ENROLL_SECRET"

workflow.sh

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
# -e: Immediately exit if any command has a non-zero exit status.
4+
# -x: Print all executed commands to the terminal.
5+
# -u: Exit if an undefined variable is used.
6+
# -o pipefail: Exit if any command in a pipeline fails.
7+
set -exuo pipefail
8+
9+
GLOBAL_FILE=./default.yml
10+
FLEETCTL="${FLEETCTL:-fleetctl}"
11+
12+
# Validate that global file contains org_settings
13+
grep -Exq "^org_settings:.*" $GLOBAL_FILE
14+
15+
if compgen -G ./teams/*.yml > /dev/null; then
16+
# Validate that every team has a unique name.
17+
# This is a limited check that assumes all team files contain the phrase: `name: <team_name>`
18+
! perl -nle 'print $1 if /^name:\s*(.+)$/' ./teams/*.yml | sort | uniq -d | grep . -cq
19+
fi
20+
21+
# Dry run
22+
$FLEETCTL gitops -f $GLOBAL_FILE --dry-run
23+
for team_file in ./teams/*.yml; do
24+
$FLEETCTL gitops -f "$team_file" --dry-run
25+
done
26+
27+
# Real run
28+
$FLEETCTL gitops -f $GLOBAL_FILE
29+
for team_file in ./teams/*.yml; do
30+
$FLEETCTL gitops -f "$team_file"
31+
done

0 commit comments

Comments
 (0)