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.



77
78
79
# File 'lib/fog/aws/iam.rb', line 77

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

Class Method Details

.dataObject



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

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

.resetObject



69
70
71
# File 'lib/fog/aws/iam.rb', line 69

def self.reset
  @data = nil
end

.server_certificate_idObject



73
74
75
# File 'lib/fog/aws/iam.rb', line 73

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

Instance Method Details

#dataObject



81
82
83
# File 'lib/fog/aws/iam.rb', line 81

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(name) ⇒ Object



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

def get_server_certificate(name)
  raise Fog::AWS::IAM::NotFound unless certificate = self.data[:server_certificates][name]

  response = Excon::Response.new
  response.status = 200
  response.body = {
    'Certificate' => certificate,
    'RequestId' => Fog::AWS::Mock.request_id
  }

  response
end

#list_server_certificates(options = {}) ⇒ Object



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

def list_server_certificates(options = {})
  certificates = self.data[:server_certificates].values
  certificates = certificates.select { |certificate| certificate['Path'] =~ Regexp.new("^#{options['PathPrefix']}") } if options['PathPrefix']
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'Certificates' => certificates
  }

  response
end

#reset_dataObject



85
86
87
# File 'lib/fog/aws/iam.rb', line 85

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
90
# 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)
    chain = OpenSSL::X509::Certificate.new(options['CertificateChain']) if options['CertificateChain']
    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 = options['path'] || "/"
    data = {
      'Arn' => Fog::AWS::Mock.arn('iam', self.data[:owner_id], "server-certificate/#{name}"),
      '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