2011-02-28 18:04:29 -05:00
module Fog
module AWS
class IAM
class Real
2011-02-28 19:00:25 -05:00
require 'fog/aws/parsers/iam/upload_server_certificate'
2011-02-28 18:04:29 -05:00
# Uploads a server certificate entity for the AWS Account.
# Includes a public key certificate, a private key, and an optional certificate chain, which should all be PEM-encoded.
#
# ==== Parameters
# * certificate<~Hash>: The contents of the public key certificate in PEM-encoded format.
# * private_key<~Hash>: The contents of the private key in PEM-encoded format.
# * name<~Hash>: The name for the server certificate. Do not include the path in this value.
# * options<~Hash>:
# * 'CertificateChain'<~String> - The contents of the certificate chain. Typically a concatenation of the PEM-encoded public key certificates of the chain.
# * 'Path'<~String> - The path for the server certificate.
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
2011-07-06 17:23:41 -04:00
# * 'Certificate'<~Hash>:
# * 'Arn'<~String> -
# * 'Path'<~String> -
# * 'ServerCertificateId'<~String> -
# * 'ServerCertificateName'<~String> -
# * 'UploadDate'<~Time>
2011-02-28 18:04:29 -05:00
# * 'RequestId'<~String> - Id of the request
#
# ==== See Also
# http://docs.amazonwebservices.com/IAM/latest/APIReference/index.html?API_UploadServerCertificate.html
#
def upload_server_certificate ( certificate , private_key , name , options = { } )
request ( {
'Action' = > 'UploadServerCertificate' ,
'CertificateBody' = > certificate ,
'PrivateKey' = > private_key ,
'ServerCertificateName' = > name ,
2011-02-28 19:00:25 -05:00
:parser = > Fog :: Parsers :: AWS :: IAM :: UploadServerCertificate . new
2011-02-28 18:04:29 -05:00
} . merge! ( options ) )
end
end
2011-07-06 17:23:41 -04:00
class Mock
def upload_server_certificate ( certificate , private_key , name , options = { } )
2011-07-20 19:04:31 -04:00
if certificate . nil? || certificate . empty? || private_key . nil? || private_key . empty?
raise Fog :: AWS :: IAM :: ValidationError . new
end
2011-07-06 17:23:41 -04:00
response = Excon :: Response . new
2011-07-20 20:24:34 -04:00
# Validate cert and key
begin
cert = OpenSSL :: X509 :: Certificate . new ( certificate )
2011-10-03 18:21:09 -04:00
chain = OpenSSL :: X509 :: Certificate . new ( options [ 'CertificateChain' ] ) if options [ 'CertificateChain' ]
2011-07-20 20:24:34 -04:00
key = OpenSSL :: PKey :: RSA . new ( private_key )
rescue OpenSSL :: X509 :: CertificateError , OpenSSL :: PKey :: RSAError = > e
2011-07-21 01:10:41 -04:00
message = if e . is_a? ( OpenSSL :: X509 :: CertificateError )
" Invalid Public Key Certificate. "
else
" Invalid Private Key. "
end
raise Fog :: AWS :: IAM :: MalformedCertificate . new ( message )
2011-07-20 20:24:34 -04:00
end
unless cert . check_private_key ( key )
raise Fog :: AWS :: IAM :: KeyPairMismatch . new
end
2011-07-06 17:23:41 -04:00
if self . data [ :server_certificates ] [ name ]
2011-07-20 20:24:34 -04:00
raise Fog :: AWS :: IAM :: EntityAlreadyExists . new
2011-07-06 17:23:41 -04:00
else
response . status = 200
2011-12-29 19:42:52 -05:00
path = options [ 'Path' ] || " / "
2011-07-06 17:23:41 -04:00
data = {
2011-09-22 05:21:08 -04:00
'Arn' = > Fog :: AWS :: Mock . arn ( 'iam' , self . data [ :owner_id ] , " server-certificate/ #{ name } " ) ,
2011-07-06 17:23:41 -04:00
'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
end
2011-02-28 18:04:29 -05:00
end
end
end