gitlab_checks 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 35eef26458f62c5c15c84cc90d176318e6817dd22dc2e18b415a09377a68fe48
4
+ data.tar.gz: ce0871a38fae398b2b7dab60d467b016d3f700065944b42aed5f74515091e399
5
+ SHA512:
6
+ metadata.gz: 8d90f4256fc5611f1c08576e1d1881a6d1508b6c285442601ef2fa614b51c9a484fcfa66edacc03d18044faee3d5bc63b2f91fbc68fde9a955aaa59e0b66dd00
7
+ data.tar.gz: 91c30f3abdcecba7ddc367301b51be8e2f8f9c03b2fee8788a750d50ca9a0bbae0fb03dca8b94e5c548ac13ee7ab18a3a94e7b6ef691f86331557085223609c6
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-01-12
4
+
5
+ - Initial framework release with initial ruleset
6
+
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in gitlab_checks.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "gitlab", "~> 4.19.0"
15
+
16
+ gem "optparse", "~> 0.3.1"
data/LICENSE.txt ADDED
@@ -0,0 +1,178 @@
1
+
2
+ Copyright 2023 Dave Satchell, Zerosource
3
+
4
+
5
+ Apache License
6
+ Version 2.0, January 2004
7
+ http://www.apache.org/licenses/
8
+
9
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
10
+
11
+ 1. Definitions.
12
+
13
+ "License" shall mean the terms and conditions for use, reproduction,
14
+ and distribution as defined by Sections 1 through 9 of this document.
15
+
16
+ "Licensor" shall mean the copyright owner or entity authorized by
17
+ the copyright owner that is granting the License.
18
+
19
+ "Legal Entity" shall mean the union of the acting entity and all
20
+ other entities that control, are controlled by, or are under common
21
+ control with that entity. For the purposes of this definition,
22
+ "control" means (i) the power, direct or indirect, to cause the
23
+ direction or management of such entity, whether by contract or
24
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
25
+ outstanding shares, or (iii) beneficial ownership of such entity.
26
+
27
+ "You" (or "Your") shall mean an individual or Legal Entity
28
+ exercising permissions granted by this License.
29
+
30
+ "Source" form shall mean the preferred form for making modifications,
31
+ including but not limited to software source code, documentation
32
+ source, and configuration files.
33
+
34
+ "Object" form shall mean any form resulting from mechanical
35
+ transformation or translation of a Source form, including but
36
+ not limited to compiled object code, generated documentation,
37
+ and conversions to other media types.
38
+
39
+ "Work" shall mean the work of authorship, whether in Source or
40
+ Object form, made available under the License, as indicated by a
41
+ copyright notice that is included in or attached to the work
42
+ (an example is provided in the Appendix below).
43
+
44
+ "Derivative Works" shall mean any work, whether in Source or Object
45
+ form, that is based on (or derived from) the Work and for which the
46
+ editorial revisions, annotations, elaborations, or other modifications
47
+ represent, as a whole, an original work of authorship. For the purposes
48
+ of this License, Derivative Works shall not include works that remain
49
+ separable from, or merely link (or bind by name) to the interfaces of,
50
+ the Work and Derivative Works thereof.
51
+
52
+ "Contribution" shall mean any work of authorship, including
53
+ the original version of the Work and any modifications or additions
54
+ to that Work or Derivative Works thereof, that is intentionally
55
+ submitted to Licensor for inclusion in the Work by the copyright owner
56
+ or by an individual or Legal Entity authorized to submit on behalf of
57
+ the copyright owner. For the purposes of this definition, "submitted"
58
+ means any form of electronic, verbal, or written communication sent
59
+ to the Licensor or its representatives, including but not limited to
60
+ communication on electronic mailing lists, source code control systems,
61
+ and issue tracking systems that are managed by, or on behalf of, the
62
+ Licensor for the purpose of discussing and improving the Work, but
63
+ excluding communication that is conspicuously marked or otherwise
64
+ designated in writing by the copyright owner as "Not a Contribution."
65
+
66
+ "Contributor" shall mean Licensor and any individual or Legal Entity
67
+ on behalf of whom a Contribution has been received by Licensor and
68
+ subsequently incorporated within the Work.
69
+
70
+ 2. Grant of Copyright License. Subject to the terms and conditions of
71
+ this License, each Contributor hereby grants to You a perpetual,
72
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
73
+ copyright license to reproduce, prepare Derivative Works of,
74
+ publicly display, publicly perform, sublicense, and distribute the
75
+ Work and such Derivative Works in Source or Object form.
76
+
77
+ 3. Grant of Patent License. Subject to the terms and conditions of
78
+ this License, each Contributor hereby grants to You a perpetual,
79
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
80
+ (except as stated in this section) patent license to make, have made,
81
+ use, offer to sell, sell, import, and otherwise transfer the Work,
82
+ where such license applies only to those patent claims licensable
83
+ by such Contributor that are necessarily infringed by their
84
+ Contribution(s) alone or by combination of their Contribution(s)
85
+ with the Work to which such Contribution(s) was submitted. If You
86
+ institute patent litigation against any entity (including a
87
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
88
+ or a Contribution incorporated within the Work constitutes direct
89
+ or contributory patent infringement, then any patent licenses
90
+ granted to You under this License for that Work shall terminate
91
+ as of the date such litigation is filed.
92
+
93
+ 4. Redistribution. You may reproduce and distribute copies of the
94
+ Work or Derivative Works thereof in any medium, with or without
95
+ modifications, and in Source or Object form, provided that You
96
+ meet the following conditions:
97
+
98
+ (a) You must give any other recipients of the Work or
99
+ Derivative Works a copy of this License; and
100
+
101
+ (b) You must cause any modified files to carry prominent notices
102
+ stating that You changed the files; and
103
+
104
+ (c) You must retain, in the Source form of any Derivative Works
105
+ that You distribute, all copyright, patent, trademark, and
106
+ attribution notices from the Source form of the Work,
107
+ excluding those notices that do not pertain to any part of
108
+ the Derivative Works; and
109
+
110
+ (d) If the Work includes a "NOTICE" text file as part of its
111
+ distribution, then any Derivative Works that You distribute must
112
+ include a readable copy of the attribution notices contained
113
+ within such NOTICE file, excluding those notices that do not
114
+ pertain to any part of the Derivative Works, in at least one
115
+ of the following places: within a NOTICE text file distributed
116
+ as part of the Derivative Works; within the Source form or
117
+ documentation, if provided along with the Derivative Works; or,
118
+ within a display generated by the Derivative Works, if and
119
+ wherever such third-party notices normally appear. The contents
120
+ of the NOTICE file are for informational purposes only and
121
+ do not modify the License. You may add Your own attribution
122
+ notices within Derivative Works that You distribute, alongside
123
+ or as an addendum to the NOTICE text from the Work, provided
124
+ that such additional attribution notices cannot be construed
125
+ as modifying the License.
126
+
127
+ You may add Your own copyright statement to Your modifications and
128
+ may provide additional or different license terms and conditions
129
+ for use, reproduction, or distribution of Your modifications, or
130
+ for any such Derivative Works as a whole, provided Your use,
131
+ reproduction, and distribution of the Work otherwise complies with
132
+ the conditions stated in this License.
133
+
134
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
135
+ any Contribution intentionally submitted for inclusion in the Work
136
+ by You to the Licensor shall be under the terms and conditions of
137
+ this License, without any additional terms or conditions.
138
+ Notwithstanding the above, nothing herein shall supersede or modify
139
+ the terms of any separate license agreement you may have executed
140
+ with Licensor regarding such Contributions.
141
+
142
+ 6. Trademarks. This License does not grant permission to use the trade
143
+ names, trademarks, service marks, or product names of the Licensor,
144
+ except as required for reasonable and customary use in describing the
145
+ origin of the Work and reproducing the content of the NOTICE file.
146
+
147
+ 7. Disclaimer of Warranty. Unless required by applicable law or
148
+ agreed to in writing, Licensor provides the Work (and each
149
+ Contributor provides its Contributions) on an "AS IS" BASIS,
150
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
151
+ implied, including, without limitation, any warranties or conditions
152
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
153
+ PARTICULAR PURPOSE. You are solely responsible for determining the
154
+ appropriateness of using or redistributing the Work and assume any
155
+ risks associated with Your exercise of permissions under this License.
156
+
157
+ 8. Limitation of Liability. In no event and under no legal theory,
158
+ whether in tort (including negligence), contract, or otherwise,
159
+ unless required by applicable law (such as deliberate and grossly
160
+ negligent acts) or agreed to in writing, shall any Contributor be
161
+ liable to You for damages, including any direct, indirect, special,
162
+ incidental, or consequential damages of any character arising as a
163
+ result of this License or out of the use or inability to use the
164
+ Work (including but not limited to damages for loss of goodwill,
165
+ work stoppage, computer failure or malfunction, or any and all
166
+ other commercial damages or losses), even if such Contributor
167
+ has been advised of the possibility of such damages.
168
+
169
+ 9. Accepting Warranty or Additional Liability. While redistributing
170
+ the Work or Derivative Works thereof, You may choose to offer,
171
+ and charge a fee for, acceptance of support, warranty, indemnity,
172
+ or other liability obligations and/or rights consistent with this
173
+ License. However, in accepting such obligations, You may act only
174
+ on Your own behalf and on Your sole responsibility, not on behalf
175
+ of any other Contributor, and only if You agree to indemnify,
176
+ defend, and hold each Contributor harmless for any liability
177
+ incurred by, or claims asserted against, such Contributor by reason
178
+ of your accepting any such warranty or additional liability.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # gitlab_checks
2
+
3
+ Security audit tool to perform common security checks on a GitLab repository.
4
+
5
+ These checks are provided to support a security audit of a GitLab.com instance.
6
+
7
+ Current checks:
8
+
9
+ | Severity | Check | Conditions |
10
+ |----------|-------------------------------------------------------------------------------------|------------|
11
+ | HIGH | Root group does not have MFA enabled | |
12
+ | MEDIUM | Sharing with groups outside the root group is allowed | Non public groups |
13
+ | MEDIUM | Billable members are present in the group that have not logged in for 60 days | |
14
+ | MEDIUM | Billable members are present in the group that have no activity in the past 90 days | |
15
+ | LOW | Root group allows users to fork repositories to outside groups | Non public groups |
16
+ | LOW | Root group does not have default branch protection set to protect the main/default branch | |
17
+ | INFORMATIONAL | Root group visibility is set to public | |
18
+
19
+
20
+ ## Installation
21
+
22
+ Install the application directly from the RubyGems repository
23
+
24
+ $ gem install gitlab_checks
25
+
26
+ A personal GitLab token
27
+
28
+ ## Usage
29
+
30
+ Provide Gitlab API endpoint, Gitlab token and Gitlab organisation id:
31
+
32
+ $ Usage: gitlab_checks --url URL --group GROUPID --token TOKEN
33
+ -u, --url URL Url of Gitlab API
34
+ -g, --group GROUPID ID of the root group for processing
35
+ -t, --token TOKEN GitLab private token or an OAuth2 access token
36
+ -h, --help Display this screen
37
+
38
+ The GitLab token can also be provided as an environment variable GITLAB_API_PRIVATE_TOKEN
39
+
40
+ For example:
41
+
42
+ $ export GITLAB_API_PRIVATE_TOKEN=glpat-....
43
+ $ gitlab_checks --url https://gitlab.com/api/v4 --group 12345678
44
+
45
+ or
46
+
47
+ $ gitlab_checks --url https://gitlab.com/api/v4 --group 12345678 --token glpat-xxx
48
+
49
+ ## Development
50
+
51
+ Download the repo:
52
+
53
+ $ git clone git@gitlab.com:zerosource/tools/gitlab_checks.git
54
+
55
+ Run directly from source:
56
+
57
+ $ bundle install
58
+ $ ruby bin/gitlab_checks --url https://gitlab.com/api/v4 --group 12345678 --token glpat-xxx
59
+
60
+ Build the gem package for installation into Ruby:
61
+
62
+ $ gem build GitlabChecks.gemspec
63
+
64
+ Install the gem by executing:
65
+
66
+ $ gem install gitlab_checks
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitLab at https://gitlab.com/zerosource/tools/gitlab_checks. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://gitlab.com/zerosource/tools/gitlab_checks/-/blob/main/CODE_OF_CONDUCT.md).
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [Apache-2.0 License](https://opensource.org/licenses/Apache-2.0).
75
+
76
+ ## Code of Conduct
77
+
78
+ Everyone interacting in the GitlabChecks project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://gitlab.com/zerosource/tools/gitlab_checks/-/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/gitlab_checks ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "gitlab_checks"
6
+
7
+ GitlabChecks::CLI::Application.new.execute
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitlabChecks
4
+ module Checks
5
+ class Group
6
+ def audit(gitlab_org)
7
+ findings = []
8
+
9
+ @group = Gitlab.group(gitlab_org.root_group)
10
+
11
+ if !!@group["require_two_factor_authentication"] != true
12
+ findings << GitlabChecks::Findings::Finding.new(GitlabChecks::Findings::SEVERITY[:HIGH],
13
+ "Two-factor authentication not required for group #{@group.name}", "To reduce the risk of unauthorised access, all users should be required to enable 2FA when joining the group")
14
+ end
15
+
16
+ if @group["default_branch_protection"] < 2
17
+ findings << GitlabChecks::Findings::Finding.new(GitlabChecks::Findings::SEVERITY[:MEDIUM],
18
+ "Default branch protection for #{@group.name} dooes not prevent merging to main branch", "Setting an appropriate value for default branch protection ensures new repositories are protected from all users writing to the default branch")
19
+ end
20
+
21
+ # checks that reduce the security of private and internal projects
22
+ if @group["visibility"] != "Public"
23
+ if !!@group["prevent_sharing_groups_outside_hierarchy"] != true
24
+ findings << GitlabChecks::Findings::Finding.new(GitlabChecks::Findings::SEVERITY[:MEDIUM],
25
+ "Sharing with groups outside the root group is allowed for group #{@group.name} (non-public group)", "Projects or groups should be shared only with groups inside the existing heirarchy to control access")
26
+ end
27
+
28
+ if !!@group["prevent_forking_outside_group"] != true
29
+ findings << GitlabChecks::Findings::Finding.new(GitlabChecks::Findings::SEVERITY[:LOW],
30
+ "Forking with outside groups is not disabled for group #{@group.name} (non-public group)", "Preventing forking with outside groups makes it more difficult for group members to copy repositories to their personal accounts or share with other projects ")
31
+ end
32
+
33
+ else
34
+ findings << GitlabChecks::Findings::Finding.new(GitlabChecks::Findings::SEVERITY[:INFORMATIONAL],
35
+ "Visibility for group #{@group.name} is public", "Public groups allow unauthenticated users to access all projects and resources by default")
36
+ end
37
+
38
+ findings
39
+
40
+ end
41
+
42
+ def output_result; end
43
+ def output_statistics; end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "gitlab_checks/findings"
4
+
5
+ module GitlabChecks
6
+ module Checks
7
+ class Members
8
+ LAST_GITLAB_ACTIVITY_THRESHOLD_DAYS = 90
9
+ LAST_GITLAB_LOGON_THRESHOLD_DAYS = 60
10
+
11
+ def initialize
12
+ @all_members = nil
13
+ @billable_members = nil
14
+ @members_last_activity_exceeded = nil
15
+ @members_last_logon_exceeded = nil
16
+ end
17
+
18
+ def audit(gitlab_org)
19
+ findings = []
20
+
21
+ @all_members = Gitlab.all_group_members(gitlab_org.root_group, { per_page: 1000 }).lazy_paginate
22
+ @billable_members = Gitlab.group_billable_members(gitlab_org.root_group,
23
+ { per_page: 1000,
24
+ sort: "last_activity_on_asc" }).lazy_paginate
25
+
26
+ @members_last_activity_exceeded = @billable_members.select do |m|
27
+ m["last_activity_on"].nil? ||
28
+ Date.parse(m["last_activity_on"]) < (Date.today - LAST_GITLAB_ACTIVITY_THRESHOLD_DAYS)
29
+ end
30
+
31
+ if @members_last_activity_exceeded.count.positive?
32
+ findings << GitlabChecks::Findings::Finding.new(GitlabChecks::Findings::SEVERITY[:MEDIUM],
33
+ "#{@members_last_activity_exceeded.count} users have had no activity within #{LAST_GITLAB_ACTIVITY_THRESHOLD_DAYS} days", "Users who have not performed recent activity on the organisation may no longer be involved with the project and continued access should be validated (see output file)")
34
+ end
35
+
36
+ @members_last_logon_exceeded = @billable_members.select do |m|
37
+ m["last_login_at"].nil? ||
38
+ Date.parse(m["last_login_at"]) < (Date.today - LAST_GITLAB_LOGON_THRESHOLD_DAYS)
39
+ end
40
+
41
+ if @members_last_logon_exceeded.count.positive?
42
+ findings << GitlabChecks::Findings::Finding.new(GitlabChecks::Findings::SEVERITY[:MEDIUM],
43
+ "#{@members_last_logon_exceeded.count} users have not logged into Gitlab within #{LAST_GITLAB_LOGON_THRESHOLD_DAYS} days", "Users who have not performed recent activity on the organisation may no longer be involved with the project and continued access should be validated (see output file)")
44
+ end
45
+ findings
46
+ end
47
+
48
+ def output_result
49
+ headers = %w[username name created_at last_activity_at last_login_at]
50
+ CSV.open("users-no-recent-activity.csv", "w") do |csv|
51
+ csv << headers
52
+ @members_last_activity_exceeded.each do |member_exceeded|
53
+ csv << [member_exceeded.username, member_exceeded.name, member_exceeded.created_at, member_exceeded.last_login_at, member_exceeded.last_activity_on]
54
+ end
55
+ end
56
+
57
+ print "Wrote users-no-recent-activity.csv..."
58
+
59
+ headers = %w[username name created_at last_activity_at last_login_at]
60
+ CSV.open("users-no-recent-login.csv", "w") do |csv|
61
+ csv << headers
62
+ @members_last_logon_exceeded.each do |member_exceeded|
63
+ csv << [member_exceeded.username, member_exceeded.name, member_exceeded.created_at, member_exceeded.last_login_at, member_exceeded.last_activity_on]
64
+ end
65
+ end
66
+
67
+ print "Wrote users-no-recent-login.csv..."
68
+ end
69
+
70
+ def output_statistics
71
+ print "Number of root group members: #{@all_members.count}"
72
+ print "Number of billable members: #{@billable_members.count}"
73
+ end
74
+
75
+ private
76
+
77
+ def print(message)
78
+ $stdout.puts message
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitlabChecks
4
+ module Checks
5
+ class Repository
6
+ def initialize
7
+ @all_repos = nil
8
+ @repos_with_gitlab_security = nil
9
+ end
10
+
11
+ def audit(gitlab_org)
12
+ findings = []
13
+
14
+ @all_repos = Gitlab.group_projects(gitlab_org.root_group,
15
+ { per_page: 100, include_subgroups: true }).lazy_paginate
16
+
17
+ @repos_with_gitlab_security = Gitlab.group_projects(gitlab_org.root_group,
18
+ { per_page: 100, include_subgroups: true,
19
+ with_security_reports: true }).lazy_paginate
20
+
21
+ findings
22
+ end
23
+
24
+ def output_result; end
25
+
26
+ def output_statistics
27
+ print "Number of projects: #{@all_repos.count}"
28
+ print "Number of projects with active Gitlab security dashboards: #{@repos_with_gitlab_security.count}"
29
+ print "Number of public projects: #{ @all_repos.select { |repo| repo["visibility"].include?("public") }.count}"
30
+ print "Number of public projects: #{ @all_repos.select { |repo| repo["visibility"].include?("internal") }.count}"
31
+ end
32
+
33
+ private
34
+
35
+ def print(message)
36
+ $stdout.puts message
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitlabChecks
4
+ module Checks
5
+ end
6
+ end
7
+
8
+ require "gitlab_checks/checks/group"
9
+ require "gitlab_checks/checks/members"
10
+ require "gitlab_checks/checks/repository"
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+ require "gitlab_checks/checks"
5
+ require "gitlab_checks/findings"
6
+
7
+ module GitlabChecks
8
+ module CLI
9
+ class Application
10
+ STATUS_SUCCESS = 0
11
+ STATUS_ERROR = 1
12
+
13
+ def initialize
14
+ @group_checks = GitlabChecks::Checks::Group.new
15
+ @member_checks = GitlabChecks::Checks::Members.new
16
+ @repo_checks = GitlabChecks::Checks::Repository.new
17
+
18
+ @findings = []
19
+
20
+ @options = {}
21
+ OptionParser.new do |opts|
22
+ opts.banner = "Usage: gitlab_checks --url URL --group GROUPID --token TOKEN"
23
+
24
+ opts.on("-u", "--url URL", "Url of Gitlab API") do |u|
25
+ @options[:endpoint] = u
26
+ end
27
+
28
+ opts.on("-g", "--group GROUPID", "ID of the root group for processing") do |u|
29
+ @options[:group] = u
30
+ end
31
+
32
+ opts.on("-t", "--token TOKEN", "GitLab private token or OAuth2 access token, default: ENV['GITLAB_API_PRIVATE_TOKEN']") do |u|
33
+ @options[:token] = u
34
+ end
35
+
36
+ opts.on("-h", "--help", "Display this screen") do
37
+ puts opts
38
+ exit
39
+ end
40
+ end.parse!
41
+ end
42
+
43
+ def execute
44
+ if @options[:endpoint].nil? || @options[:group].nil?
45
+ print("Usage: gitlab_checks --url URL --group GROUPID --token TOKEN")
46
+ exit(STATUS_ERROR)
47
+ end
48
+
49
+ if @options[:token].nil? && ENV['GITLAB_API_PRIVATE_TOKEN'].nil?
50
+ print("No GitLab token specified, please use --token TOKEN or set env variable GITLAB_API_PRIVATE_TOKEN")
51
+ exit(STATUS_ERROR)
52
+ end
53
+
54
+ print "-----------------------------------"
55
+ print "GITLAB CHECKS v#{GitlabChecks::VERSION} by Zerosource (https://zerosource.io)"
56
+ print "-----------------------------------"
57
+ print "Running checks on GitLab group: #{@options[:group]}"
58
+ print ""
59
+
60
+ gitlab_org = GitlabChecks::Connector::Organisation.new(@options[:endpoint], @options[:token], @options[:group])
61
+ do_audit(gitlab_org)
62
+ do_stats(gitlab_org)
63
+ do_output(gitlab_org)
64
+ STATUS_SUCCESS
65
+ end
66
+
67
+ def do_audit(gitlab_org)
68
+ @findings.concat(@group_checks.audit(gitlab_org))
69
+ @findings.concat(@member_checks.audit(gitlab_org) || [])
70
+ @findings.concat(@repo_checks.audit(gitlab_org) || [])
71
+
72
+ @findings.sort_by!(&:severity)
73
+ print "-----------------------------------"
74
+ print "FINDINGS"
75
+ print "-----------------------------------"
76
+ print ""
77
+ @findings.each(&:print_console)
78
+ end
79
+
80
+ def do_stats(_gitlab_org)
81
+ print "-----------------------------------"
82
+ print "STATISTICS"
83
+ print "-----------------------------------"
84
+ print ""
85
+ @group_checks.output_statistics
86
+ @repo_checks.output_statistics
87
+ @member_checks.output_statistics
88
+ print ""
89
+ end
90
+
91
+ def do_output(gitlab_org)
92
+ print "-----------------------------------"
93
+ print "OUTPUT"
94
+ print "-----------------------------------"
95
+ print ""
96
+ @group_checks.output_result
97
+ @member_checks.output_result
98
+ @repo_checks.output_result
99
+ print ""
100
+ end
101
+
102
+ private
103
+
104
+ def print(message)
105
+ $stdout.puts message
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitlabChecks
4
+ module CLI
5
+ end
6
+ end
7
+
8
+ require "gitlab_checks/cli/application"
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "gitlab"
4
+
5
+ module GitlabChecks
6
+ module Connector
7
+ class Organisation
8
+ def initialize(_endpoint, _token, _group)
9
+ @root_group = nil
10
+ @subgroups = nil
11
+ @member = nil
12
+ @repositories = nil
13
+
14
+ Gitlab.configure do |config|
15
+ config.endpoint = _endpoint
16
+ config.private_token = _token if !_token.nil?
17
+ config.user_agent = "Gitlab Checks v#{GitlabChecks::VERSION} (https://gitlab.com/zerosource/tools/gitlab_checks)"
18
+ end
19
+
20
+ @root_group = _group
21
+ end
22
+
23
+ attr_reader :root_group
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitlabChecks
4
+ module Connector
5
+ end
6
+ end
7
+
8
+ require "gitlab_checks/connector/organisation"
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitlabChecks
4
+ module Findings
5
+
6
+ SEVERITY = { CRITICAL: 1, HIGH: 2, MEDIUM: 3, LOW: 4, INFORMATIONAL: 5 }.freeze
7
+
8
+ class Finding
9
+ def initialize(_severity, _title, _description)
10
+ @severity = _severity
11
+ @title = _title
12
+ @description = _description
13
+ end
14
+
15
+ def severity_to_string(severity)
16
+ SEVERITY.key(severity)
17
+ end
18
+
19
+ def print_console
20
+ puts "(#{severity_to_string(@severity)}) - #{@title}"
21
+ puts " #{@description}"
22
+ puts ""
23
+ end
24
+
25
+ attr_reader :severity, :title, :description
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitlabChecks
4
+ module Findings
5
+ end
6
+ end
7
+
8
+ require "gitlab_checks/findings/finding"
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitlabChecks
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitlabChecks
4
+ class Error < StandardError; end
5
+ end
6
+
7
+ require "gitlab"
8
+ require "gitlab_checks/cli"
9
+ require "gitlab_checks/checks"
10
+ require "gitlab_checks/connector"
11
+ require "gitlab_checks/findings"
12
+ require "gitlab_checks/version"
@@ -0,0 +1,13 @@
1
+ module GitlabChecks
2
+ module Checks
3
+ class Group
4
+ @group: Array[Gitlab::Client::Groups]
5
+
6
+ def audit: -> Array[Findings::Finding]
7
+
8
+ def output_result: -> void
9
+
10
+ def output_statistics: -> void
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+
2
+ module GitlabChecks
3
+ module Checks
4
+ class Members
5
+ LAST_GITLAB_ACTIVITY_THRESHOLD_DAYS: Integer
6
+
7
+ LAST_GITLAB_LOGON_THRESHOLD_DAYS: Integer
8
+
9
+ @test: OpenSSL::ASN1::Integer
10
+ @all_members: Array[Gitlab::Client::Groups]
11
+ @billable_members: Array[Gitlab::Client::Groups]
12
+ @members_last_activity_exceeded: Array[Gitlab::Client::Groups]
13
+ @members_last_logon_exceeded: Array[Gitlab::Client::Groups]
14
+
15
+ def audit: -> Array[Findings::Finding]
16
+
17
+ def output_result: -> void
18
+
19
+ def output_statistics: -> void
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ module GitlabChecks
2
+ module Checks
3
+ class Repository
4
+ @all_repos: untyped
5
+ @repos_with_gitlab_security: untyped
6
+
7
+ def audit: -> Array[Findings::Finding]
8
+
9
+ def output_result: -> void
10
+
11
+ def output_statistics: -> void
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ module GitlabChecks
2
+ module CLI
3
+ class Application
4
+ STATUS_ERROR: Integer
5
+ STATUS_SUCCESS: Integer
6
+
7
+ @findings: Array[Findings::Finding]
8
+ @group_checks: GitlabChecks::Checks::Group
9
+ @member_checks: GitlabChecks::Checks::Members
10
+ @options: Hash
11
+ @repo_checks: GitlabChecks::Checks::Repository
12
+
13
+ def do_audit: -> void
14
+
15
+ def do_output: -> void
16
+
17
+ def do_stats: -> void
18
+
19
+ def execute: -> void
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+
2
+ module GitlabChecks
3
+ module Connector
4
+ class Organisation
5
+ @member: untyped
6
+ @repositories: untyped
7
+ @subgroups: untyped
8
+
9
+ attr_reader root_group: Integer
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,22 @@
1
+ module GitlabChecks
2
+ module Findings
3
+ SEVERITY: Hash[Symbol, Integer]
4
+
5
+ class Finding
6
+
7
+ @description: String
8
+ @title: String
9
+ @severity: GitlabChecks::Findings::SEVERITY
10
+
11
+ attr_reader description: String
12
+ attr_reader title: String
13
+ attr_reader severity: GitlabChecks::Findings::SEVERITY
14
+
15
+ def print_console: -> void
16
+
17
+ def severity_to_string: -> String
18
+
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ module GitlabChecks
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitlab_checks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Zerosource
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gitlab
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.19.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.19.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: optparse
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.1
41
+ description: Perform a number of common security checks against your GitLab organisation
42
+ email: hello@zerosource.io
43
+ executables:
44
+ - gitlab_checks
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rubocop.yml"
49
+ - CHANGELOG.md
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/gitlab_checks
56
+ - lib/gitlab_checks.rb
57
+ - lib/gitlab_checks/checks.rb
58
+ - lib/gitlab_checks/checks/group.rb
59
+ - lib/gitlab_checks/checks/members.rb
60
+ - lib/gitlab_checks/checks/repository.rb
61
+ - lib/gitlab_checks/cli.rb
62
+ - lib/gitlab_checks/cli/application.rb
63
+ - lib/gitlab_checks/connector.rb
64
+ - lib/gitlab_checks/connector/organisation.rb
65
+ - lib/gitlab_checks/findings.rb
66
+ - lib/gitlab_checks/findings/finding.rb
67
+ - lib/gitlab_checks/version.rb
68
+ - sig/gitlab_checks.rbs
69
+ - sig/gitlab_checks/checks/group.rbs
70
+ - sig/gitlab_checks/checks/members.rbs
71
+ - sig/gitlab_checks/checks/repository.rbs
72
+ - sig/gitlab_checks/cli/application.rbs
73
+ - sig/gitlab_checks/connector/organisation.rbs
74
+ - sig/gitlab_checks/findings/finding.rbs
75
+ homepage: https://zerosource.io
76
+ licenses:
77
+ - Apache-2.0
78
+ metadata:
79
+ homepage_uri: https://zerosource.io
80
+ source_code_uri: https://gitlab.com/zerosource/tools/gitlab_checks
81
+ changelog_uri: https://gitlab.com/zerosource/tools/CHANGELOG.md
82
+ bug_tracker_uri: https://gitlab.com/zerosource/tools/gitlab_checks/issues
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.6.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.0.3.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Security checks for GitLab organisations and repositories
102
+ test_files: []