Class: Fog::AWS::KMS::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/kms.rb,
lib/fog/aws/requests/kms/list_keys.rb,
lib/fog/aws/requests/kms/create_key.rb,
lib/fog/aws/requests/kms/describe_key.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



45
46
47
48
49
50
51
52
53
# File 'lib/fog/aws/kms.rb', line 45

def initialize(options={})
  @use_iam_profile = options[:use_iam_profile]
  @account_id = Fog::AWS::Mock.owner_id

  @region = options[:region] || 'us-east-1'
  setup_credentials(options)

  Fog::AWS.validate_region!(@region)
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



43
44
45
# File 'lib/fog/aws/kms.rb', line 43

def 
  @account_id
end

Class Method Details

.dataObject



29
30
31
32
33
34
35
36
37
# File 'lib/fog/aws/kms.rb', line 29

def self.data
  @data ||= Hash.new do |hash, region|
    hash[region] = Hash.new do |region_hash, access_key|
      region_hash[access_key] = {
        :keys => {},
      }
    end
  end
end

.resetObject



39
40
41
# File 'lib/fog/aws/kms.rb', line 39

def self.reset
  data.clear
end

Instance Method Details

#create_key(policy = nil, description = nil, usage = "ENCRYPT_DECRYPT") ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fog/aws/requests/kms/create_key.rb', line 37

def create_key(policy = nil, description = nil, usage = "ENCRYPT_DECRYPT")
  response = Excon::Response.new
  key_id   = UUID.uuid
  key_arn  = Fog::AWS::Mock.arn("kms", self., "key/#{key_id}", @region)

  key = {
    "KeyUsage"     => usage,
    "AWSAccountId" => self.,
    "KeyId"        => key_id,
    "Description"  => description,
    "CreationDate" => Time.now,
    "Arn"          => key_arn,
    "Enabled"      => true,
  }

  # @todo use default policy

  self.data[:keys][key_id] = key

  response.body = { "KeyMetadata" => key }
  response
end

#dataObject



62
63
64
# File 'lib/fog/aws/kms.rb', line 62

def data
  self.class.data[@region][@aws_access_key_id]
end

#describe_key(identifier) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/fog/aws/requests/kms/describe_key.rb', line 17

def describe_key(identifier)
  response = Excon::Response.new
  key = self.data[:keys][identifier]

  response.body = { "KeyMetadata" => key }
  response
end

#list_keys(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fog/aws/requests/kms/list_keys.rb', line 27

def list_keys(options={})
  limit  = options[:limit]
  marker = options[:marker]

  if limit
    if limit > 1_000
      raise Fog::AWS::KMS::Error.new(
        "ValidationError => 1 validation error detected: Value '#{limit}' at 'limit' failed to satisfy constraint: Member must have value less than or equal to 1000"
      )
    elsif limit <  1
      raise Fog::AWS::KMS::Error.new(
        "ValidationError => 1 validation error detected: Value '#{limit}' at 'limit' failed to satisfy constraint: Member must have value greater than or equal to 1"
      )
    end
  end

  key_set = if marker
              self.data[:markers][marker] || []
            else
              self.data[:keys].inject([]) { |r,(k,v)|
                r << { "KeyId" => k, "KeyArn" => v["Arn"] }
              }
            end

  keys = if limit
           key_set.slice!(0, limit)
         else
           key_set
         end

  truncated = keys.size < key_set.size

  marker = truncated && "metadata/l/#{}/#{UUID.uuid}"

  response = Excon::Response.new

  body = {
    'Keys'      => keys,
    'Truncated' => truncated,
    'RequestId' => Fog::AWS::Mock.request_id
  }

  if marker
    self.data[:markers][marker] = key_set
    body.merge!('Marker' => marker)
  end

  response.body = body
  response.status = 200

  response
end

#reset_dataObject



66
67
68
# File 'lib/fog/aws/kms.rb', line 66

def reset_data
  self.class.data[@region].delete(@aws_access_key_id)
end

#setup_credentials(options) ⇒ Object



55
56
57
58
59
60
# File 'lib/fog/aws/kms.rb', line 55

def setup_credentials(options)
  @aws_access_key_id     = options[:aws_access_key_id]
  @aws_secret_access_key = options[:aws_secret_access_key]

  @signer = Fog::AWS::SignatureV4.new(@aws_access_key_id, @aws_secret_access_key, @region, 'kms')
end