Class: SoarAuthenticationToken::ConfigRotator

Inherits:
Object
  • Object
show all
Defined in:
lib/soar_authentication_token/config_rotator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigRotator

Returns a new instance of ConfigRotator.



7
8
9
# File 'lib/soar_authentication_token/config_rotator.rb', line 7

def initialize
  @maximum_number_of_public_keys = 3
end

Instance Attribute Details

#maximum_number_of_public_keysObject

Returns the value of attribute maximum_number_of_public_keys.



5
6
7
# File 'lib/soar_authentication_token/config_rotator.rb', line 5

def maximum_number_of_public_keys
  @maximum_number_of_public_keys
end

Instance Method Details

#configurations_match_and_valid?(generator_config:, validator_config:) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/soar_authentication_token/config_rotator.rb', line 51

def configurations_match_and_valid?(generator_config:,validator_config:)
  validate_generator_config(generator_config)
  validate_validator_config(validator_config)
  test_token = generate_test_token(generator_config)
  validate_test_token(validator_config,test_token)
end

#rotate_configs(generator_config:, validator_config:) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/soar_authentication_token/config_rotator.rb', line 26

def rotate_configs(generator_config:, validator_config:)
  private_key, public_key = KeypairGenerator.new.generate
  key_description = generate_keypair_description
  updated_generator_config = rotate_generator_config(config: generator_config,new_private_key: private_key,new_key_description: key_description)
  updated_validator_config = rotate_validator_config(config: validator_config,new_public_key:  public_key, new_key_description: key_description)
  raise 'generated configuration does not match' unless configurations_match_and_valid?(generator_config: updated_generator_config,validator_config: updated_validator_config)
  [updated_generator_config, updated_validator_config]
end

#rotate_generator_config(config:, new_private_key:, new_key_description:) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/soar_authentication_token/config_rotator.rb', line 35

def rotate_generator_config(config: ,new_private_key: ,new_key_description:)
  validate_generator_config(config)
  new_config = config.dup
  new_config['auth_token_generator']['private_key'] = new_private_key
  new_config['auth_token_generator']['key_description'] = new_key_description
  new_config
end

#rotate_json_config_files(generator_file_name:, validator_file_name:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/soar_authentication_token/config_rotator.rb', line 11

def rotate_json_config_files(generator_file_name:, validator_file_name:)
  generator_config = JSON.parse(File.read(generator_file_name))
  validator_config = JSON.parse(File.read(validator_file_name))
  generator_config, validator_config = rotate_configs(generator_config: generator_config,
                                                      validator_config: validator_config)
  File.open(generator_file_name,"w") do |f|
    f.write(JSON.pretty_generate generator_config)
    f.close
  end
  File.open(validator_file_name,"w") do |f|
    f.write(JSON.pretty_generate validator_config)
    f.close
  end
end

#rotate_validator_config(config:, new_public_key:, new_key_description:) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/soar_authentication_token/config_rotator.rb', line 43

def rotate_validator_config(config: ,new_public_key: ,new_key_description:)
  validate_validator_config(config)
  new_config = config.dup
  trim_public_keys(new_config)
  new_config['auth_token_validator']['keys'][new_key_description] = { 'public_key' => new_public_key }
  new_config
end