Skip to content

Commit 7b6d720

Browse files
author
Vilnius Ramanauskas
committed
New flow handling
1 parent 26cf666 commit 7b6d720

File tree

12 files changed

+139
-47
lines changed

12 files changed

+139
-47
lines changed

REFERENCE.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* [`Gitlab_ci_runner::Keyserver`](#Gitlab_ci_runner--Keyserver): Type to match repo_keyserver Regex from: https://github.com/puppetlabs/puppetlabs-apt/blob/main/manifests/key.pp
3535
* [`Gitlab_ci_runner::Log_format`](#Gitlab_ci_runner--Log_format): Gitlab Runner log format configuration
3636
* [`Gitlab_ci_runner::Log_level`](#Gitlab_ci_runner--Log_level): Gitlab Runner log level configuration
37-
* [`Gitlab_ci_runner::Register`](#Gitlab_ci_runner--Register): A struct of all possible additionl options for gitlab_ci_runner::register
37+
* [`Gitlab_ci_runner::Register`](#Gitlab_ci_runner--Register): A struct of all possible additional options for gitlab_ci_runner::register
3838
* [`Gitlab_ci_runner::Register_parameters`](#Gitlab_ci_runner--Register_parameters): A enum containing a possible keys used for Gitlab runner registrations
3939
* [`Gitlab_ci_runner::Session_server`](#Gitlab_ci_runner--Session_server): Gitlab Runner session_server configuration
4040

@@ -503,7 +503,7 @@ puppet apply -e "notice(gitlab_ci_runner::register('https://gitlab.com', 'regist
503503

504504
A function that registers a Gitlab runner on a Gitlab instance. Be careful, this will be triggered on noop runs as well!
505505

506-
Returns: `Struct[{ id => Integer[1], token => String[1], }]` Returns a hash with the runner id and authentcation token
506+
Returns: `Struct[{ id => Integer[1], token => String[1], }]` Returns a hash with the runner id and authentication token
507507

508508
##### Examples
509509

@@ -673,7 +673,7 @@ puppet apply -e "notice(gitlab_ci_runner::unregister('https://gitlab.com', 'runn
673673

674674
A function that unregisters a Gitlab runner from a Gitlab instance. Be careful, this will be triggered on noop runs as well!
675675

676-
Returns: `Struct[{ status => Enum['success'], }]` Returns a hash with the runner id and authentcation token
676+
Returns: `Struct[{ status => Enum['success'], }]` Returns status
677677

678678
##### Examples
679679

@@ -724,7 +724,7 @@ file { '/etc/gitlab-runner/auth-token-testrunner':
724724
A function that unregisters a Gitlab runner from a Gitlab instance, if the local token is there.
725725
This is meant to be used in conjunction with the gitlab_ci_runner::register_to_file function.
726726

727-
Returns: `Any`
727+
Returns: `String` Returns status
728728

729729
##### Examples
730730

@@ -747,7 +747,7 @@ The url to your Gitlab instance. Please only provide the host part (e.g https://
747747

748748
Data type: `String[1]`
749749

750-
The name of the runner. Use as identifier for the retrived auth token.
750+
The name of the runner. Use as identifier for the retrieved auth token.
751751

752752
##### `proxy`
753753

@@ -784,7 +784,7 @@ Alias of `Enum['debug', 'info', 'warn', 'error', 'fatal', 'panic']`
784784

785785
### <a name="Gitlab_ci_runner--Register"></a>`Gitlab_ci_runner::Register`
786786

787-
A struct of all possible additionl options for gitlab_ci_runner::register
787+
A struct of all possible additional options for gitlab_ci_runner::register
788788

789789
Alias of
790790

lib/puppet/functions/gitlab_ci_runner/register.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# @param token Registration token.
99
# @param additional_options A hash with all additional configuration options for that runner
1010
# @param ca_file An absolute path to a trusted certificate authority file.
11-
# @return [Struct[{ id => Integer[1], token => String[1], }]] Returns a hash with the runner id and authentcation token
11+
# @return [Struct[{ id => Integer[1], token => String[1], }]] Returns a hash with the runner id and authentication token
1212
# @example Using it as a replacement for the Bolt 'register_runner' task
1313
# puppet apply -e "notice(gitlab_ci_runner::register('https://gitlab.com', 'registration-token'))"
1414
#
@@ -21,7 +21,11 @@
2121
end
2222

2323
def register(url, token, additional_options = {}, ca_file = nil)
24-
PuppetX::Gitlab::Runner.register(url, additional_options.merge('token' => token), ca_file: ca_file)
24+
if token.start_with?('glrt-')
25+
PuppetX::Gitlab::Runner.verify(url, token, ca_file: ca_file)
26+
else
27+
PuppetX::Gitlab::Runner.register(url, additional_options.merge('registration-token' => token), ca_file: ca_file)
28+
end
2529
rescue Net::HTTPError => e
2630
raise "Gitlab runner failed to register: #{e.message}"
2731
end

lib/puppet/functions/gitlab_ci_runner/register_to_file.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ def register_to_file(url, regtoken, runner_name, additional_options = {}, proxy
5151
# will be returned unmodified.
5252
regtoken = call_function('unwrap', regtoken)
5353

54-
authtoken = PuppetX::Gitlab::Runner.register(url, additional_options.merge('token' => regtoken), proxy, ca_file)['token']
54+
# Combine options based on the token
55+
if regtoken.start_with?('glrt-')
56+
PuppetX::Gitlab::Runner.verify(url, regtoken, proxy, ca_file)
57+
authtoken = regtoken
58+
else
59+
authtoken = PuppetX::Gitlab::Runner.register(url, additional_options.merge('registration-token' => regtoken), proxy, ca_file)['token']
60+
end
5561

5662
# If this function is used as a Deferred function the Gitlab Runner config dir
5763
# will not exist on the first run, because the package isn't installed yet.

lib/puppet/functions/gitlab_ci_runner/unregister.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# @param url The url to your Gitlab instance. Please only provide the host part (e.g https://gitlab.com)
99
# @param token Runners authentication token.
1010
# @param ca_file An absolute path to a trusted certificate authority file.
11-
# @return [Struct[{ id => Integer[1], token => String[1], }]] Returns a hash with the runner id and authentcation token
11+
# @return [Struct[{ status => String[1], }]] Returns status
1212
# @example Using it as a replacement for the Bolt 'unregister_runner' task
1313
# puppet apply -e "notice(gitlab_ci_runner::unregister('https://gitlab.com', 'runner-auth-token'))"
1414
#

lib/puppet/functions/gitlab_ci_runner/unregister_from_file.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
# This is meant to be used in conjunction with the gitlab_ci_runner::register_to_file function.
77
Puppet::Functions.create_function(:'gitlab_ci_runner::unregister_from_file') do
88
# @param url The url to your Gitlab instance. Please only provide the host part (e.g https://gitlab.com)
9-
# @param runner_name The name of the runner. Use as identifier for the retrived auth token.
9+
# @param runner_name The name of the runner. Use as identifier for the retrieved auth token.
1010
# @param proxy HTTP proxy to use when unregistering
1111
# @param ca_file An absolute path to a trusted certificate authority file.
12+
# @return [String] Returns status
1213
# @example Using it as a Deferred function with a file resource
1314
# file { '/etc/gitlab-runner/auth-token-testrunner':
1415
# file => absent,

lib/puppet_x/gitlab/runner.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ def self.register(host, options, proxy = nil, ca_file = nil)
7070
PuppetX::Gitlab::APIClient.post(url, options, proxy, ca_file)
7171
end
7272

73+
def self.verify(host, token, proxy = nil, ca_file = nil)
74+
url = "#{host}/api/v4/runners/verify"
75+
Puppet.info "Verifying gitlab runner with #{host}"
76+
PuppetX::Gitlab::APIClient.post(url, { 'token' => token }, proxy, ca_file)
77+
end
78+
7379
def self.unregister(host, options, proxy = nil, ca_file = nil)
7480
url = "#{host}/api/v4/runners"
7581
Puppet.info "Unregistering gitlab runner with #{host}"

manifests/runner.pp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,14 @@
8484
default => $config,
8585
}
8686

87-
if $_config['registration-token'] {
87+
if $_config['registration-token'] or $_config['token'] {
8888
$register_additional_options = $config
8989
.filter |$item| { $item[0] =~ Gitlab_ci_runner::Register_parameters } # Get all items use for the registration process
9090
.reduce({}) |$memo, $item| { $memo + { regsubst($item[0], '-', '_', 'G') => $item[1] } } # Ensure all keys use '_' instead of '-'
9191

92-
$deferred_call = Deferred('gitlab_ci_runner::register_to_file', [$_config['url'], $_config['registration-token'], $_config['name'], $register_additional_options, $http_proxy, $ca_file])
92+
$token = pick($_config['token'], $_config['registration-token'])
93+
94+
$deferred_call = Deferred('gitlab_ci_runner::register_to_file', [$_config['url'], $token, $_config['name'], $register_additional_options, $http_proxy, $ca_file])
9395

9496
# Remove registration-token and add a 'token' key to the config with a Deferred function to get it.
9597
$__config = ($_config - (Array(Gitlab_ci_runner::Register_parameters) + 'registration-token')) + { 'token' => $deferred_call }

metadata.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "puppet-gitlab_ci_runner",
3-
"version": "5.1.1-rc0",
3+
"version": "5.2.0-rc0",
44
"author": "Vox Pupuli",
55
"summary": "Installation and configuration of Gitlab CI Runner",
66
"license": "Apache-2.0",
@@ -64,7 +64,8 @@
6464
{
6565
"operatingsystem": "Debian",
6666
"operatingsystemrelease": [
67-
"11"
67+
"11",
68+
"12"
6869
]
6970
},
7071
{

spec/functions/register_spec.rb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
describe 'gitlab_ci_runner::register' do
77
let(:url) { 'https://gitlab.example.org' }
88
let(:regtoken) { 'registration-token' }
9+
let(:auth_token) { 'glrt-authentication-token' }
910
let(:return_hash) do
1011
{
1112
'id' => 1234,
@@ -20,17 +21,28 @@
2021
it { is_expected.to run.with_params('https://gitlab.com', 1234).and_raise_error(ArgumentError) }
2122
it { is_expected.to run.with_params('https://gitlab.com', 'registration-token', project: 1234).and_raise_error(ArgumentError) }
2223

23-
it "calls 'PuppetX::Gitlab::Runner.register'" do
24-
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'token' => regtoken }, ca_file: nil).and_return(return_hash)
24+
context 'with registration token' do
25+
it "calls 'PuppetX::Gitlab::Runner.register'" do
26+
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'registration-token' => regtoken }, ca_file: nil).and_return(return_hash)
2527

26-
is_expected.to run.with_params(url, regtoken).and_return(return_hash)
27-
expect(PuppetX::Gitlab::Runner).to have_received(:register)
28+
is_expected.to run.with_params(url, regtoken).and_return(return_hash)
29+
expect(PuppetX::Gitlab::Runner).to have_received(:register)
30+
end
31+
32+
it "passes additional args to 'PuppetX::Gitlab::Runner.register'" do
33+
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'registration-token' => regtoken, 'active' => false }, ca_file: nil).and_return(return_hash)
34+
35+
is_expected.to run.with_params(url, regtoken, 'active' => false).and_return(return_hash)
36+
expect(PuppetX::Gitlab::Runner).to have_received(:register)
37+
end
2838
end
2939

30-
it "passes additional args to 'PuppetX::Gitlab::Runner.register'" do
31-
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'token' => regtoken, 'active' => false }, ca_file: nil).and_return(return_hash)
40+
context 'with authentication token' do
41+
it "calls 'PuppetX::Gitlab::Runner.verify'" do
42+
allow(PuppetX::Gitlab::Runner).to receive(:verify).with(url, auth_token, ca_file: nil).and_return(return_hash)
3243

33-
is_expected.to run.with_params(url, regtoken, 'active' => false).and_return(return_hash)
34-
expect(PuppetX::Gitlab::Runner).to have_received(:register)
44+
is_expected.to run.with_params(url, auth_token).and_return(return_hash)
45+
expect(PuppetX::Gitlab::Runner).to have_received(:verify)
46+
end
3547
end
3648
end

spec/functions/register_to_file_spec.rb

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55

66
describe 'gitlab_ci_runner::register_to_file' do
77
let(:url) { 'https://gitlab.example.org' }
8-
let(:regtoken) { 'registration-token' }
8+
let(:token) do
9+
{
10+
reg_token: 'registration-token',
11+
auth_token: 'glrt-authentication-token'
12+
}
13+
end
914
let(:runner_name) { 'testrunner' }
1015
let(:filename) { "/etc/gitlab-runner/auth-token-#{runner_name}" }
1116
let(:return_hash) do
@@ -30,42 +35,81 @@
3035
allow(File).to receive(:read).with(filename).and_return(return_hash['token'])
3136
end
3237

33-
it { is_expected.to run.with_params(url, regtoken, runner_name).and_return(return_hash['token']) }
38+
it { is_expected.to run.with_params(url, token[:reg_token], runner_name).and_return(return_hash['token']) }
39+
it { is_expected.to run.with_params(url, token[:auth_token], runner_name).and_return(return_hash['token']) }
3440
end
3541

36-
context "retrieves from Gitlab and writes auth token to file if it doesn't exist" do
42+
context 'retrieves from Gitlab and writes auth token to file if it doesn\'t exist' do
3743
before do
38-
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'token' => regtoken }, nil, nil).and_return(return_hash)
3944
allow(File).to receive(:exist?).and_call_original
4045
allow(File).to receive(:exist?).with(File.dirname(filename)).and_return(true)
41-
allow(File).to receive(:write).with(filename, return_hash['token'])
4246
allow(File).to receive(:chmod).with(0o400, filename)
4347
end
4448

45-
it { is_expected.to run.with_params(url, regtoken, runner_name).and_return(return_hash['token']) }
46-
47-
context 'with existing file ca_file option' do
49+
context 'with registration token' do
4850
before do
49-
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'token' => regtoken }, nil, '/tmp').and_return(return_hash)
51+
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'registration-token' => token[:reg_token] }, nil, nil).and_return(return_hash)
52+
allow(File).to receive(:write).with(filename, return_hash['token'])
5053
end
5154

52-
it { is_expected.to run.with_params(url, regtoken, runner_name, {}, nil, '/tmp').and_return(return_hash['token']) }
53-
end
55+
it { is_expected.to run.with_params(url, token[:reg_token], runner_name).and_return(return_hash['token']) }
5456

55-
context 'with non existent ca_file option' do
56-
before do
57-
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'token' => regtoken }, nil, '/path/to/ca_file').and_return(return_hash)
57+
context 'with existing file ca_file option' do
58+
before do
59+
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'registration-token' => token[:reg_token] }, nil, '/tmp').and_return(return_hash)
60+
end
61+
62+
it { is_expected.to run.with_params(url, token[:reg_token], runner_name, {}, nil, '/tmp').and_return(return_hash['token']) }
5863
end
5964

60-
it { is_expected.to run.with_params(url, regtoken, runner_name, {}, nil, '/path/to/ca_file').and_return('Specified CA file doesn\'t exist, not attempting to create authtoken') }
65+
context 'with non existent ca_file option' do
66+
before do
67+
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'registration-token' => token[:reg_token] }, nil, '/path/to/ca_file').and_return(return_hash)
68+
end
69+
70+
it { is_expected.to run.with_params(url, token[:reg_token], runner_name, {}, nil, '/path/to/ca_file').and_return('Specified CA file doesn\'t exist, not attempting to create authtoken') }
71+
end
72+
73+
context 'with sensitive token value' do
74+
before do
75+
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'registration-token' => token[:reg_token] }, nil, '/tmp').and_return(return_hash)
76+
end
77+
78+
it { is_expected.to run.with_params(url, sensitive(token[:reg_token]), runner_name, {}, nil, '/tmp').and_return(return_hash['token']) }
79+
end
6180
end
6281

63-
context 'with sensitive token value' do
82+
context 'with authentication token' do
6483
before do
65-
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'token' => regtoken }, nil, '/tmp').and_return(return_hash)
84+
allow(PuppetX::Gitlab::Runner).to receive(:verify).with(url, token[:auth_token], nil, nil).and_return(return_hash.merge('token' => token[:auth_token]))
85+
allow(File).to receive(:write).with(filename, token[:auth_token])
86+
end
87+
88+
it { is_expected.to run.with_params(url, token[:auth_token], runner_name).and_return(token[:auth_token]) }
89+
90+
context 'with existing file ca_file option' do
91+
before do
92+
allow(PuppetX::Gitlab::Runner).to receive(:verify).with(url, token[:auth_token], nil, '/tmp').and_return(return_hash.merge('token' => token[:auth_token]))
93+
end
94+
95+
it { is_expected.to run.with_params(url, token[:auth_token], runner_name, {}, nil, '/tmp').and_return(token[:auth_token]) }
6696
end
6797

68-
it { is_expected.to run.with_params(url, sensitive(regtoken), runner_name, {}, nil, '/tmp').and_return(return_hash['token']) }
98+
context 'with non existent ca_file option' do
99+
before do
100+
allow(PuppetX::Gitlab::Runner).to receive(:verify).with(url, token[:auth_token], nil, '/path/to/ca_file').and_return(return_hash.merge('token' => token[:auth_token]))
101+
end
102+
103+
it { is_expected.to run.with_params(url, token[:auth_token], runner_name, {}, nil, '/path/to/ca_file').and_return('Specified CA file doesn\'t exist, not attempting to create authtoken') }
104+
end
105+
106+
context 'with sensitive token value' do
107+
before do
108+
allow(PuppetX::Gitlab::Runner).to receive(:verify).with(url, token[:auth_token], nil, '/tmp').and_return(return_hash.merge('token' => token[:auth_token]))
109+
end
110+
111+
it { is_expected.to run.with_params(url, sensitive(token[:auth_token]), runner_name, {}, nil, '/tmp').and_return(token[:auth_token]) }
112+
end
69113
end
70114
end
71115

@@ -75,6 +119,7 @@
75119
allow(Puppet.settings).to receive(:[]).with(:noop).and_return(true)
76120
end
77121

78-
it { is_expected.to run.with_params(url, regtoken, runner_name).and_return('DUMMY-NOOP-TOKEN') }
122+
it { is_expected.to run.with_params(url, token[:reg_token], runner_name).and_return('DUMMY-NOOP-TOKEN') }
123+
it { is_expected.to run.with_params(url, token[:auth_token], runner_name).and_return('DUMMY-NOOP-TOKEN') }
79124
end
80125
end

0 commit comments

Comments
 (0)