-
Notifications
You must be signed in to change notification settings - Fork 995
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #23210 - Handle PuppetCA tokens
In a new SmartProxy PuppetCA autosigning variant tokens get returned that need to be provisioned on the host.
- Loading branch information
Julian Todt
committed
Jul 9, 2018
1 parent
d666c6c
commit 7075e12
Showing
12 changed files
with
172 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Hostext | ||
module Puppetca | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
has_one :puppetca_token, :foreign_key => :host_id, :dependent => :destroy, :inverse_of => :host, :class_name => 'Token::Puppetca' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Token::Build < ::Token | ||
validates :expires, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Token::Puppetca < ::Token | ||
validates :value, uniqueness: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class AddTypeToToken < ActiveRecord::Migration[5.1] | ||
def up | ||
remove_foreign_key :tokens, :column => :host_id if foreign_key_exists?(:tokens, { :name => "tokens_host_id_fk" }) | ||
remove_index :tokens, :host_id if index_exists? :tokens, :host_id # was unique | ||
add_index :tokens, :host_id | ||
add_foreign_key :tokens, :hosts, :name => "tokens_host_id_fk" unless foreign_key_exists?(:tokens, { :name => "tokens_host_id_fk" }) | ||
add_column :tokens, :type, :string, default: 'Token::Build', null: false, index: true | ||
change_column :tokens, :value, :string, limit: 900 | ||
end | ||
|
||
def down | ||
change_column :tokens, :value, :string, limit: 255 | ||
remove_column :tokens, :type | ||
remove_foreign_key :tokens, :column => :host_id if foreign_key_exists?(:tokens, { :name => "tokens_host_id_fk" }) | ||
remove_index :tokens, :host_id if index_exists? :tokens, :host_id | ||
add_index :tokens, :host_id, :unique => true | ||
add_foreign_key :tokens, :hosts, :name => "tokens_host_id_fk" unless foreign_key_exists?(:tokens, { :name => "tokens_host_id_fk" }) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'test_helper' | ||
|
||
class Token::BuildTest < ActiveSupport::TestCase | ||
should validate_presence_of(:expires) | ||
|
||
let(:host) { FactoryBot.create(:host) } | ||
|
||
test "a host can create a token" do | ||
host.create_token(:value => "aaaaaa", :expires => Time.now.utc) | ||
assert_equal Token.first.value, "aaaaaa" | ||
assert_equal Token.first.host_id, host.id | ||
end | ||
|
||
test "a host can delete its token" do | ||
host.create_token(:value => 'aaaaaa', :expires => Time.now.utc + 1.minute) | ||
assert_instance_of Token::Build, host.token | ||
host.token = nil | ||
assert Token.where(:value => 'aaaaaa', :host_id => host.id).empty? | ||
end | ||
|
||
test "a host cannot delete tokens for other hosts" do | ||
host2 = FactoryBot.create(:host) | ||
host.create_token(:value => 'aaaaaa', :expires => Time.now.utc + 1.minute) | ||
host2.create_token(:value => 'bbbbbb', :expires => Time.now.utc + 1.minute) | ||
assert_equal Token.all.size, 2 | ||
host.token = nil | ||
assert_equal Token.all.size, 1 | ||
end | ||
|
||
test "not all expired tokens should be removed" do | ||
host2 = FactoryBot.create(:host) | ||
host.create_token(:value => 'aaaaaa', :expires => Time.now.utc + 1.minute) | ||
host2.create_token(:value => 'bbbbbb', :expires => Time.now.utc - 1.minute) | ||
assert_equal 2, Token.count | ||
host.expire_token | ||
assert_equal 1, Token.count | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'test_helper' | ||
|
||
class Token::PuppetcaTest < ActiveSupport::TestCase | ||
should validate_uniqueness_of(:value) | ||
|
||
let(:host) { FactoryBot.create(:host) } | ||
|
||
test "a host can create a puppetca-token" do | ||
host.create_puppetca_token value: 'foo.bar.baz' | ||
assert_instance_of Token::Puppetca, host.puppetca_token | ||
assert_equal Token::Puppetca.first.host_id, host.id | ||
assert_equal 'foo.bar.baz', host.puppetca_token.value | ||
end | ||
|
||
test "a host can delete its puppetca-token" do | ||
host.create_puppetca_token value: 'aaaa' | ||
assert_equal host.puppetca_token.value, 'aaaa' | ||
host.puppetca_token = nil | ||
assert_nil host.puppetca_token | ||
assert_equal Token::Puppetca.all, [] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters