module Rundeck::Client::Key

Defines methods related to projects.

Constants

STORAGE_KEYS_PATH

The endpoint for key storage

Public Instance Methods

create_private_key(path, key, options = {}) click to toggle source

Create a private key

@example

key = "-----BEGIN RSA PRIVATE KEY-----\nProc-Type:..."
Rundeck.create_private_key('path', key)

@param [String] path A key storage path @param [String] key The entire private key value @!macro options @return [Rundeck::ObjectifiedHash] @!macro exceptions

# File lib/rundeck/client/key.rb, line 95
def create_private_key(path, key, options = {})
  create_or_update_key(path, key, 'private', 'post', options)
end
create_public_key(path, key, options = {}) click to toggle source

Create a public key

@example

key = "ssh-rsa AAAA.....3MOj user@example.com"
Rundeck.create_public_key('path/to/key', key)

@param [String] path A key storage path @param [String] key The entire private key value @!macro options @return [Rundeck::ObjectifiedHash] @!macro exceptions

# File lib/rundeck/client/key.rb, line 127
def create_public_key(path, key, options = {})
  create_or_update_key(path, key, 'public', 'post', options)
end
delete_key(path, options = {}) click to toggle source

Delete a key

@example

Rundeck.delete_key('path/to/key')

@param [String] path A key storage path @!macro options @return [nil] @!macro exceptions

# File lib/rundeck/client/key.rb, line 157
def delete_key(path, options = {})
  delete("#{STORAGE_KEYS_PATH}/#{path}", options)
end
key_contents(path, options = {}) click to toggle source

Get the contents of a key. Only allowed for public keys. Note: This method returns a raw string of the public key, not at ObjectifiedHash.

@example

Rundeck.key_contents('path/to/key1')

@param [String] path A key storage path, including key name @!macro options @return [Rundeck::ObjectifiedHash] @!macro exceptions

# File lib/rundeck/client/key.rb, line 68
def key_contents(path, options = {})
  # Check if key exists first. Otherwise we could return some
  # weird strings. Also, raise error if user is trying to get a
  # private key.
  key_content_type = key_metadata(path, options).rundeck_content_type
  if key_content_type == content_type('private')
    fail Error::Unauthorized,
         'You are not allowed to retrieve the contents of a private key'
  end

  options.merge!(format: :plain,
                 headers: { 'Accept' => 'application/pgp-keys' })
  key = get("#{STORAGE_KEYS_PATH}/#{path}", options)
  objectify 'public_key' => key
end
key_metadata(path, options = {}) click to toggle source

Get a single key’s metadata

@example

Rundeck.key_metadata('path/to/key1')

@param [String] path A key storage path, including key name @!macro options @return [Rundeck::ObjectifiedHash] @!macro exceptions @raise [Rundeck::Error::InvalidAttributes] if the path is a direct key

path
# File lib/rundeck/client/key.rb, line 44
def key_metadata(path, options = {})
  r = get("#{STORAGE_KEYS_PATH}/#{path}", options)

  # In case a user provides a key path instead of a path to a single key.
  if r['resource']['contents']
    fail Error::InvalidAttributes,
         'Please provide a key storage path that ' \
         'is a direct path to a key'
  else
    objectify r['resource']['resource_meta']
  end
end
keys(path = '', options = {}) click to toggle source

Gets a list of keys at a specific path.

@example

Rundeck.keys('path')

@param [String] path A key storage path @!macro options @return [Array<Rundeck::ObjectifiedHash>] @!macro exceptions @raise [Rundeck::Error::InvalidAttributes] if the path is a direct key

path
# File lib/rundeck/client/key.rb, line 20
def keys(path = '', options = {})
  r = get("#{STORAGE_KEYS_PATH}/#{path}", options)

  # In case a user provides a direct path to a key, error.
  if r['resource']['contents']
    objectify r['resource']['contents']['resource']
  else
    fail Error::InvalidAttributes,
         'Please provide a key storage path that ' \
         'isn\'t a direct path to a key'
  end
end
update_private_key(path, key, options = {}) click to toggle source

Update a private key

@example

key = "-----BEGIN RSA PRIVATE KEY-----\nProc-Type:..."
Rundeck.update_private_key('path', key)

@param [String] path A key storage path @param [String] key The entire private key value @!macro options @return [Rundeck::ObjectifiedHash] @!macro exceptions @raise [Rundeck::Error::NotFound] if the key was not found

# File lib/rundeck/client/key.rb, line 111
def update_private_key(path, key, options = {})
  key_check(path, 'private', options)
  create_or_update_key(path, key, 'private', 'put', options)
end
update_public_key(path, key, options = {}) click to toggle source

Update a public key

@example

key = "ssh-rsa AAAA.....3MOj user@example.com"
Rundeck.update_public_key('path/to/key', key)

@param [String] path A key storage path @param [String] key The entire private key value @!macro options @return [Rundeck::ObjectifiedHash] @!macro exceptions @raise [Rundeck::Error::NotFound] if the key was not foun

# File lib/rundeck/client/key.rb, line 143
def update_public_key(path, key, options = {})
  key_check(path, 'public', options)
  create_or_update_key(path, key, 'public', 'put', options)
end

Private Instance Methods

content_type(key_type) click to toggle source
# File lib/rundeck/client/key.rb, line 187
def content_type(key_type)
  if key_type == 'private'
    'application/octet-stream'
  elsif key_type == 'public'
    'application/pgp-key'
  else
    fail Error::InvalidAttributes,
         'Invalid key type specified. Must be public or private.'
  end
end
create_or_update_key(path, key, type, method, options) click to toggle source
# File lib/rundeck/client/key.rb, line 172
def create_or_update_key(path, key, type, method, options)
  key_type_headers(type, options)
  options.merge!(body: key)

  if method == 'post'
    objectify post("#{STORAGE_KEYS_PATH}/#{path}", options)['resource']['resource_meta']
  elsif method == 'put'
    objectify put("#{STORAGE_KEYS_PATH}/#{path}", options)['resource']['resource_meta']
  end
end
key_check(path, key_type, options) click to toggle source
# File lib/rundeck/client/key.rb, line 163
def key_check(path, key_type, options)
  content_type = content_type(key_type)
  # Check existence and type
  if key_metadata(path, options).rundeck_content_type != content_type
    fail Error::NotFound,
         "A #{type} key was not found at the specified path."
  end
end
key_type_headers(type, options) click to toggle source
# File lib/rundeck/client/key.rb, line 183
def key_type_headers(type, options)
  options.merge!(headers: { 'Content-Type' => content_type(type) })
end