Class: Fog::AWS::IAM::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/iam.rb,
lib/fog/aws/requests/iam/get_server_certificate.rb,
lib/fog/aws/requests/iam/list_server_certificates.rb,
lib/fog/aws/requests/iam/delete_server_certificate.rb,
lib/fog/aws/requests/iam/upload_server_certificate.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



75
76
77
# File 'lib/fog/aws/iam.rb', line 75

def initialize(options={})
  @aws_access_key_id = options[:aws_access_key_id]
end

Class Method Details

.dataObject



58
59
60
61
62
63
64
65
# File 'lib/fog/aws/iam.rb', line 58

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :owner_id => Fog::AWS::Mock.owner_id,
      :server_certificates => {}
    }
  end
end

.resetObject



67
68
69
# File 'lib/fog/aws/iam.rb', line 67

def self.reset
  @data = nil
end

.server_certificate_idObject



71
72
73
# File 'lib/fog/aws/iam.rb', line 71

def self.server_certificate_id
  Fog::Mock.random_hex(16)
end

Instance Method Details

#dataObject



79
80
81
# File 'lib/fog/aws/iam.rb', line 79

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

#delete_server_certificate(server_certificate_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/aws/requests/iam/delete_server_certificate.rb', line 32

def delete_server_certificate(server_certificate_name)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'RequestId' => Fog::AWS::Mock.request_id
  }

  self.data[:server_certificates].delete(server_certificate_name)

  response
end

#get_server_certificate(server_certificate_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/aws/requests/iam/get_server_certificate.rb', line 32

def get_server_certificate(server_certificate_name)
  raise Fog::AWS::IAM::NotFound unless self.data[:server_certificates].key?(server_certificate_name)

  response = Excon::Response.new
  response.status = 200
  response.body = {
    'Certificate' => self.data[:server_certificates][server_certificate_name],
    'RequestId' => Fog::AWS::Mock.request_id
  }

  self.data[:server_certificates]

  response
end

#list_server_certificates(options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/fog/aws/requests/iam/list_server_certificates.rb', line 42

def list_server_certificates(options = {})
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'Certificates' => self.data[:server_certificates].values
  }

  response
end

#reset_dataObject



83
84
85
# File 'lib/fog/aws/iam.rb', line 83

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

#upload_server_certificate(certificate, private_key, name, options = {}) ⇒ Object



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
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fog/aws/requests/iam/upload_server_certificate.rb', line 46

def upload_server_certificate(certificate, private_key, name, options = {})
  if certificate.nil? || certificate.empty? || private_key.nil? || private_key.empty?
    raise Fog::AWS::IAM::ValidationError.new
  end
  response = Excon::Response.new

  # Validate cert and key
  begin
    cert = OpenSSL::X509::Certificate.new(certificate)
    key = OpenSSL::PKey::RSA.new(private_key)
  rescue OpenSSL::X509::CertificateError, OpenSSL::PKey::RSAError => e
    message = if e.is_a?(OpenSSL::X509::CertificateError)
                "Invalid Public Key Certificate."
              else
                "Invalid Private Key."
              end
    raise Fog::AWS::IAM::MalformedCertificate.new(message)
  end

  unless cert.check_private_key(key)
    raise Fog::AWS::IAM::KeyPairMismatch.new
  end

  if self.data[:server_certificates][name]
    raise Fog::AWS::IAM::EntityAlreadyExists.new
  else
    response.status = 200
    path = "server-certificates/#{name}"
    data = {
      'Arn' => Fog::AWS::Mock.arn('iam', self.data[:owner_id], path),
      'Path' => path,
      'ServerCertificateId' => Fog::AWS::IAM::Mock.server_certificate_id,
      'ServerCertificateName' => name,
      'UploadDate' => Time.now
    }
    self.data[:server_certificates][name] = data
    response.body = {
      'Certificate' => data,
      'RequestId' => Fog::AWS::Mock.request_id
    }
  end

  response
end