class Awful::Kms

Constants

COLORS

Public Instance Methods

alias_by_name(name) click to toggle source

return target id for alias

# File lib/awful/kms.rb, line 38
def alias_by_name(name)
  list_aliases.find do |a|
    a.alias_name == "alias/#{name}"
  end.target_key_id
end
aliases() click to toggle source
# File lib/awful/kms.rb, line 122
def aliases
  list_aliases.output do |list|
    if options[:long]
      print_table list.map { |a| [a.alias_name, a.target_key_id, a.alias_arn] }
    else
      puts list.map(&:alias_name)
    end
  end
end
aliases_hash() click to toggle source
# File lib/awful/kms.rb, line 31
def aliases_hash
  @_aliases_hash ||= list_aliases.each_with_object({}) do |a, h|
    h[a.target_key_id] = a.alias_name.gsub(/^alias\//, '')
  end
end
color(string) click to toggle source
# File lib/awful/kms.rb, line 21
def color(string)
  set_color(string, COLORS.fetch(string.to_sym, :yellow))
end
decrypt(data) click to toggle source
# File lib/awful/kms.rb, line 92
def decrypt(data)
  key = Base64.decode64(data)
  puts kms.decrypt(ciphertext_blob: key)
end
encrypt(id, data) click to toggle source
# File lib/awful/kms.rb, line 86
def encrypt(id, data)
  blob = kms.encrypt(key_id: id, plaintext: data).ciphertext_blob
  puts Base64.encode64(blob)
end
get(id) click to toggle source
# File lib/awful/kms.rb, line 71
def get(id)
  kms.describe_key(key_id: id_or_alias(id)).key_metadata.output do |key|
    puts YAML.dump(stringify_keys(key.to_hash))
  end
end
id(name) click to toggle source
# File lib/awful/kms.rb, line 133
def id(name)
  alias_by_name(name).output(&method(:puts))
end
id_or_alias(id) click to toggle source
# File lib/awful/kms.rb, line 48
def id_or_alias(id)
  is_uuid?(id) ? id : alias_by_name(id)
end
is_uuid?(id) click to toggle source
# File lib/awful/kms.rb, line 44
def is_uuid?(id)
  id.match(/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i)
end
kms() click to toggle source
# File lib/awful/kms.rb, line 17
def kms
  @_kms ||= Aws::KMS::Client.new
end
list_aliases() click to toggle source
# File lib/awful/kms.rb, line 25
def list_aliases
  paginate(:aliases) do |marker|
    kms.list_aliases(marker: marker)
  end
end
ls() click to toggle source
# File lib/awful/kms.rb, line 55
def ls
  paginate(:keys) do |marker|
    kms.list_keys(marker: marker)
  end.output do |keys|
    if options[:long]
      print_table keys.map { |k|
        key = kms.describe_key(key_id: k.key_id).key_metadata
        [ aliases_hash.fetch(k.key_id, '-'), k.key_id, color(key.key_state), key.creation_date ]
      }.sort
    else
      puts keys.map(&:key_id)
    end
  end
end
policy(id) click to toggle source
# File lib/awful/kms.rb, line 79
def policy(id)
  kms.get_key_policy(key_id: id_or_alias(id), policy_name: options[:name]).policy.output do |policy|
    puts policy
  end
end
tag(id, *tags) click to toggle source
# File lib/awful/kms.rb, line 98
def tag(id, *tags)
  kms.tag_resource(
      key_id: id_or_alias(id),
      tags: tags.map do |tag|
        k,v = tag.split(/[:=]/)
        {tag_key: k, tag_value: v}
      end
    )
end
tags(id) click to toggle source
# File lib/awful/kms.rb, line 109
def tags(id)
  paginate(:tags) do |marker|
    kms.list_resource_tags(
      key_id: id_or_alias(id),
      next_marker: marker,
    )
  end.output do |tags|
    print_table tags.map(&:to_a)
  end
end