Class: Plus4uCodebase::Security

Inherits:
Object
  • Object
show all
Defined in:
lib/plus4u_codebase/security.rb,
lib/plus4u_codebase/security/ssh_key.rb,
lib/plus4u_codebase/security/ssh_key_type.rb

Defined Under Namespace

Classes: SSHKey, SSHKeyType

Constant Summary

DEFAULT_TYPE =
'rsa'
DEFAULT_BITS =
2048

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, options = nil) ⇒ Security

Creates a new instance of Security.

Parameters:

  • session (UU::OS::Security::Session)

    Session to use for remote calls.

  • options (Hash) (defaults to: nil)

    Options for UU::OS::CMD::CommandClient.



20
21
22
# File 'lib/plus4u_codebase/security.rb', line 20

def initialize(session, options = nil)
  @cmd = UU::OS::CMD::CommandClient.new("plus4u-codebase", session, options)
end

Class Method Details

.add_key(uu_uri, params = {}) ⇒ void

This method returns an undefined value.

Add public ssh key for actual logged user. Public key is valid for all hubs and repositories in same territory, this means that you don't need to set key for every repository but set it only once. Only one key per user is allowed, so if you set more keys for same user, the last one is used.

Examples:

Set from existing ssh key


# log as user for which we set key
UU::OS::Security::Session.('...')

# read existing public key
public_ssh_key = File.read('/path/to/key/13-666-1.pub')

# set ssh key
Plus4uCodebase::Security.add_key('ues:TER:HUB', key: public_ssh_key)

Generate new ssh key


# generate key with comment
key = Plus4uCodebase::Security.generate_key(comment: '13-666-1')

# set ssh key
Plus4uCodebase::Security.add_key('ues:TER:HUB', key: key.public_key)

# save private part of key used for ssh access
File.open('my_private_key', 'w') do |file|
  file.write(key.private_key)
end

Parameters:

  • uu_uri (String, UU::OS::UESURI)

    URI of the hub or repository.

  • params (Hash) (defaults to: {})

    Ssh key attributes.

Options Hash (params):

  • :key (String)

    Public key as String.



113
114
115
116
# File 'lib/plus4u_codebase/security.rb', line 113

def self.add_key(uu_uri, params = {})
  session = UU::OS::Security::Session.current_session
  self.new(session).add_key(uu_uri, params)
end

.generate_key(params = {}) ⇒ Plus4uCodebase::Security::SSHKey

Generate a new ssh keypair. The default key type is 2048-bit RSA.

Examples:

# generate ssh key pair as default 2048-bit RSA, with no comment or passphrase
key = Plus4uCodebase::Security.generate_key

key.bits # 2048
key.type # 'rsa'
key.public_key # String 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ...'
key.private_key # String '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAxVu3AQL9M4Fk...'

# generate ssh key pair as default 2048-bit RSA, with comment
key = Plus4uCodebase::Security.generate_key(comment: '13-666-1')

# generate ssh key pair as 4096-bit RSA, with comment and passphrase
key = Plus4uCodebase::Security.generate_key(
  type: Plus4uCodebase::Security::SSHKeyType::RSA,
  bits: 4096,
  comment: '13-666-1',
  passphrase: 'my_passphrase'
 )

Parameters:

  • params (Hash) (defaults to: {})

    Ssh key params.

Options Hash (params):

  • :type (String)

    Type of keys, can be one from SSHKeyType. Default is ‘RSA’.

  • :bits (Fixnum)

    Determine the strength of the key in bits as an integer. Default is 2048.

  • :comment (String)

    Shh key identification.

  • :passphrase (String)

    Ssh key passphrase.

Returns:



164
165
166
167
168
169
170
171
172
# File 'lib/plus4u_codebase/security.rb', line 164

def self.generate_key(params = {})
  opts = {}
  opts[:type] = params[:type] || DEFAULT_TYPE
  opts[:bits] = params[:bits] || DEFAULT_BITS
  opts[:comment] = params[:comment] if params[:comment]
  opts[:passphrase] = params[:passphrase] if params[:passphrase]
  key = ::SSHKey.generate(opts)
  return Plus4uCodebase::Security::SSHKey.new(key)
end

.remove_key(uu_uri) ⇒ void

This method returns an undefined value.

Remove public ssh key for actual logged user. Public key is removed for all hubs and repositories in same territory, this means that you don't need to remove key for every repository or hub.

Examples:

log as user for which we want to remove key
UU::OS::Security::Session.login('...')

remove key for actual user
Plus4uCodebase::Security.remove('ues:TER:HUB')

Parameters:

  • uu_uri (String, UU::OS::UESURI)

    URI of the hub or repository.



131
132
133
134
# File 'lib/plus4u_codebase/security.rb', line 131

def self.remove_key(uu_uri)
  session = UU::OS::Security::Session.current_session
  self.new(session).remove_key(uu_uri)
end

Instance Method Details

#add_key(uu_uri, params = {}) ⇒ void

This method returns an undefined value.

Add public ssh key for actual logged user. Public key is valid for all hubs and repositories in same territory, this means that you don't need to set key for every repository but set it only once. Only one key per user is allowed, so if you set more keys for same user, the last one is used.

Examples:

Set from existing ssh key


# log as user for which we set key
UU::OS::Security::Session.('...')

# read existing public key
public_ssh_key = File.read('/path/to/key/13-666-1.pub')

# set ssh key
security = Plus4uCodebase::Security.new(UU::OS::Security::Session.current_session)
security.add_key('ues:TER:HUB', key: public_ssh_key)

Generate new ssh key


# generate key with comment
key = Plus4uCodebase::Security.generate_key(comment: '13-666-1')

# set ssh key
security = Plus4uCodebase::Security.new(UU::OS::Security::Session.current_session)
security.add_key('ues:TER:HUB', key: key.public_key)

# save private part of key used for ssh access
File.open('my_private_key', 'w') do |file|
  file.write(key.private_key)
end

Parameters:

  • uu_uri (String, UU::OS::UESURI)

    URI of the hub or repository.

  • params (Hash) (defaults to: {})

    Ssh key attributes.

Options Hash (params):

  • :key (String)

    Public key as String.



58
59
60
61
62
# File 'lib/plus4u_codebase/security.rb', line 58

def add_key(uu_uri, params = {})
  ssh_key = input(params)
  validate_ssh_key(ssh_key)
  @cmd.invoke('Security/addKey', uu_uri, parameters: {sshKey: ssh_key})
end

#remove_key(uu_uri) ⇒ void

This method returns an undefined value.

Remove public ssh key for actual logged user. Public key is removed for all hubs and repositories in same territory, this means that you don't need to remove key for every repository or hub.

Examples:

log as user for which we want to remove key
UU::OS::Security::Session.login('...')

# remove key for actual user
security = Plus4uCodebase::Security.new(UU::OS::Security::Session.current_session)
security.remove('ues:TER:HUB')

Parameters:

  • uu_uri (String, UU::OS::UESURI)

    URI of the hub or repository.



77
78
79
# File 'lib/plus4u_codebase/security.rb', line 77

def remove_key(uu_uri)
  @cmd.invoke('Security/removeKey', uu_uri)
end