{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.KMS.GenerateRandom
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Returns a random byte string that is cryptographically secure.
--
-- By default, the random byte string is generated in KMS. To generate the
-- byte string in the CloudHSM cluster that is associated with a
-- <https://docs.aws.amazon.com/kms/latest/developerguide/custom-key-store-overview.html custom key store>,
-- specify the custom key store ID.
--
-- Applications in Amazon Web Services Nitro Enclaves can call this
-- operation by using the
-- <https://github.com/aws/aws-nitro-enclaves-sdk-c Amazon Web Services Nitro Enclaves Development Kit>.
-- For information about the supporting parameters, see
-- <https://docs.aws.amazon.com/kms/latest/developerguide/services-nitro-enclaves.html How Amazon Web Services Nitro Enclaves use KMS>
-- in the /Key Management Service Developer Guide/.
--
-- For more information about entropy and random number generation, see
-- <https://docs.aws.amazon.com/kms/latest/cryptographic-details/ Key Management Service Cryptographic Details>.
--
-- __Required permissions__:
-- <https://docs.aws.amazon.com/kms/latest/developerguide/kms-api-permissions-reference.html kms:GenerateRandom>
-- (IAM policy)
module Amazonka.KMS.GenerateRandom
  ( -- * Creating a Request
    GenerateRandom (..),
    newGenerateRandom,

    -- * Request Lenses
    generateRandom_numberOfBytes,
    generateRandom_customKeyStoreId,

    -- * Destructuring the Response
    GenerateRandomResponse (..),
    newGenerateRandomResponse,

    -- * Response Lenses
    generateRandomResponse_plaintext,
    generateRandomResponse_httpStatus,
  )
where

import qualified Amazonka.Core as Core
import Amazonka.KMS.Types
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude
import qualified Amazonka.Request as Request
import qualified Amazonka.Response as Response

-- | /See:/ 'newGenerateRandom' smart constructor.
data GenerateRandom = GenerateRandom'
  { -- | The length of the byte string.
    GenerateRandom -> Maybe Natural
numberOfBytes :: Prelude.Maybe Prelude.Natural,
    -- | Generates the random byte string in the CloudHSM cluster that is
    -- associated with the specified
    -- <https://docs.aws.amazon.com/kms/latest/developerguide/custom-key-store-overview.html custom key store>.
    -- To find the ID of a custom key store, use the DescribeCustomKeyStores
    -- operation.
    GenerateRandom -> Maybe Text
customKeyStoreId :: Prelude.Maybe Prelude.Text
  }
  deriving (GenerateRandom -> GenerateRandom -> Bool
(GenerateRandom -> GenerateRandom -> Bool)
-> (GenerateRandom -> GenerateRandom -> Bool) -> Eq GenerateRandom
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GenerateRandom -> GenerateRandom -> Bool
$c/= :: GenerateRandom -> GenerateRandom -> Bool
== :: GenerateRandom -> GenerateRandom -> Bool
$c== :: GenerateRandom -> GenerateRandom -> Bool
Prelude.Eq, ReadPrec [GenerateRandom]
ReadPrec GenerateRandom
Int -> ReadS GenerateRandom
ReadS [GenerateRandom]
(Int -> ReadS GenerateRandom)
-> ReadS [GenerateRandom]
-> ReadPrec GenerateRandom
-> ReadPrec [GenerateRandom]
-> Read GenerateRandom
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GenerateRandom]
$creadListPrec :: ReadPrec [GenerateRandom]
readPrec :: ReadPrec GenerateRandom
$creadPrec :: ReadPrec GenerateRandom
readList :: ReadS [GenerateRandom]
$creadList :: ReadS [GenerateRandom]
readsPrec :: Int -> ReadS GenerateRandom
$creadsPrec :: Int -> ReadS GenerateRandom
Prelude.Read, Int -> GenerateRandom -> ShowS
[GenerateRandom] -> ShowS
GenerateRandom -> String
(Int -> GenerateRandom -> ShowS)
-> (GenerateRandom -> String)
-> ([GenerateRandom] -> ShowS)
-> Show GenerateRandom
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GenerateRandom] -> ShowS
$cshowList :: [GenerateRandom] -> ShowS
show :: GenerateRandom -> String
$cshow :: GenerateRandom -> String
showsPrec :: Int -> GenerateRandom -> ShowS
$cshowsPrec :: Int -> GenerateRandom -> ShowS
Prelude.Show, (forall x. GenerateRandom -> Rep GenerateRandom x)
-> (forall x. Rep GenerateRandom x -> GenerateRandom)
-> Generic GenerateRandom
forall x. Rep GenerateRandom x -> GenerateRandom
forall x. GenerateRandom -> Rep GenerateRandom x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GenerateRandom x -> GenerateRandom
$cfrom :: forall x. GenerateRandom -> Rep GenerateRandom x
Prelude.Generic)

-- |
-- Create a value of 'GenerateRandom' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'numberOfBytes', 'generateRandom_numberOfBytes' - The length of the byte string.
--
-- 'customKeyStoreId', 'generateRandom_customKeyStoreId' - Generates the random byte string in the CloudHSM cluster that is
-- associated with the specified
-- <https://docs.aws.amazon.com/kms/latest/developerguide/custom-key-store-overview.html custom key store>.
-- To find the ID of a custom key store, use the DescribeCustomKeyStores
-- operation.
newGenerateRandom ::
  GenerateRandom
newGenerateRandom :: GenerateRandom
newGenerateRandom =
  GenerateRandom' :: Maybe Natural -> Maybe Text -> GenerateRandom
GenerateRandom'
    { $sel:numberOfBytes:GenerateRandom' :: Maybe Natural
numberOfBytes = Maybe Natural
forall a. Maybe a
Prelude.Nothing,
      $sel:customKeyStoreId:GenerateRandom' :: Maybe Text
customKeyStoreId = Maybe Text
forall a. Maybe a
Prelude.Nothing
    }

-- | The length of the byte string.
generateRandom_numberOfBytes :: Lens.Lens' GenerateRandom (Prelude.Maybe Prelude.Natural)
generateRandom_numberOfBytes :: (Maybe Natural -> f (Maybe Natural))
-> GenerateRandom -> f GenerateRandom
generateRandom_numberOfBytes = (GenerateRandom -> Maybe Natural)
-> (GenerateRandom -> Maybe Natural -> GenerateRandom)
-> Lens
     GenerateRandom GenerateRandom (Maybe Natural) (Maybe Natural)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GenerateRandom' {Maybe Natural
numberOfBytes :: Maybe Natural
$sel:numberOfBytes:GenerateRandom' :: GenerateRandom -> Maybe Natural
numberOfBytes} -> Maybe Natural
numberOfBytes) (\s :: GenerateRandom
s@GenerateRandom' {} Maybe Natural
a -> GenerateRandom
s {$sel:numberOfBytes:GenerateRandom' :: Maybe Natural
numberOfBytes = Maybe Natural
a} :: GenerateRandom)

-- | Generates the random byte string in the CloudHSM cluster that is
-- associated with the specified
-- <https://docs.aws.amazon.com/kms/latest/developerguide/custom-key-store-overview.html custom key store>.
-- To find the ID of a custom key store, use the DescribeCustomKeyStores
-- operation.
generateRandom_customKeyStoreId :: Lens.Lens' GenerateRandom (Prelude.Maybe Prelude.Text)
generateRandom_customKeyStoreId :: (Maybe Text -> f (Maybe Text))
-> GenerateRandom -> f GenerateRandom
generateRandom_customKeyStoreId = (GenerateRandom -> Maybe Text)
-> (GenerateRandom -> Maybe Text -> GenerateRandom)
-> Lens GenerateRandom GenerateRandom (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GenerateRandom' {Maybe Text
customKeyStoreId :: Maybe Text
$sel:customKeyStoreId:GenerateRandom' :: GenerateRandom -> Maybe Text
customKeyStoreId} -> Maybe Text
customKeyStoreId) (\s :: GenerateRandom
s@GenerateRandom' {} Maybe Text
a -> GenerateRandom
s {$sel:customKeyStoreId:GenerateRandom' :: Maybe Text
customKeyStoreId = Maybe Text
a} :: GenerateRandom)

instance Core.AWSRequest GenerateRandom where
  type
    AWSResponse GenerateRandom =
      GenerateRandomResponse
  request :: GenerateRandom -> Request GenerateRandom
request = Service -> GenerateRandom -> Request GenerateRandom
forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON Service
defaultService
  response :: Logger
-> Service
-> Proxy GenerateRandom
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse GenerateRandom)))
response =
    (Int
 -> ResponseHeaders
 -> Object
 -> Either String (AWSResponse GenerateRandom))
-> Logger
-> Service
-> Proxy GenerateRandom
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse GenerateRandom)))
forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> Object -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveJSON
      ( \Int
s ResponseHeaders
h Object
x ->
          Maybe (Sensitive Base64) -> Int -> GenerateRandomResponse
GenerateRandomResponse'
            (Maybe (Sensitive Base64) -> Int -> GenerateRandomResponse)
-> Either String (Maybe (Sensitive Base64))
-> Either String (Int -> GenerateRandomResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Either String (Maybe (Sensitive Base64))
forall a. FromJSON a => Object -> Text -> Either String (Maybe a)
Core..?> Text
"Plaintext")
            Either String (Int -> GenerateRandomResponse)
-> Either String Int -> Either String GenerateRandomResponse
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Int -> Either String Int
forall (f :: * -> *) a. Applicative f => a -> f a
Prelude.pure (Int -> Int
forall a. Enum a => a -> Int
Prelude.fromEnum Int
s))
      )

instance Prelude.Hashable GenerateRandom

instance Prelude.NFData GenerateRandom

instance Core.ToHeaders GenerateRandom where
  toHeaders :: GenerateRandom -> ResponseHeaders
toHeaders =
    ResponseHeaders -> GenerateRandom -> ResponseHeaders
forall a b. a -> b -> a
Prelude.const
      ( [ResponseHeaders] -> ResponseHeaders
forall a. Monoid a => [a] -> a
Prelude.mconcat
          [ HeaderName
"X-Amz-Target"
              HeaderName -> ByteString -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# ( ByteString
"TrentService.GenerateRandom" ::
                          Prelude.ByteString
                      ),
            HeaderName
"Content-Type"
              HeaderName -> ByteString -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# ( ByteString
"application/x-amz-json-1.1" ::
                          Prelude.ByteString
                      )
          ]
      )

instance Core.ToJSON GenerateRandom where
  toJSON :: GenerateRandom -> Value
toJSON GenerateRandom' {Maybe Natural
Maybe Text
customKeyStoreId :: Maybe Text
numberOfBytes :: Maybe Natural
$sel:customKeyStoreId:GenerateRandom' :: GenerateRandom -> Maybe Text
$sel:numberOfBytes:GenerateRandom' :: GenerateRandom -> Maybe Natural
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"NumberOfBytes" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Natural -> Pair) -> Maybe Natural -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Natural
numberOfBytes,
            (Text
"CustomKeyStoreId" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (Text -> Pair) -> Maybe Text -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
customKeyStoreId
          ]
      )

instance Core.ToPath GenerateRandom where
  toPath :: GenerateRandom -> ByteString
toPath = ByteString -> GenerateRandom -> ByteString
forall a b. a -> b -> a
Prelude.const ByteString
"/"

instance Core.ToQuery GenerateRandom where
  toQuery :: GenerateRandom -> QueryString
toQuery = QueryString -> GenerateRandom -> QueryString
forall a b. a -> b -> a
Prelude.const QueryString
forall a. Monoid a => a
Prelude.mempty

-- | /See:/ 'newGenerateRandomResponse' smart constructor.
data GenerateRandomResponse = GenerateRandomResponse'
  { -- | The random byte string. When you use the HTTP API or the Amazon Web
    -- Services CLI, the value is Base64-encoded. Otherwise, it is not
    -- Base64-encoded.
    GenerateRandomResponse -> Maybe (Sensitive Base64)
plaintext :: Prelude.Maybe (Core.Sensitive Core.Base64),
    -- | The response's http status code.
    GenerateRandomResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (GenerateRandomResponse -> GenerateRandomResponse -> Bool
(GenerateRandomResponse -> GenerateRandomResponse -> Bool)
-> (GenerateRandomResponse -> GenerateRandomResponse -> Bool)
-> Eq GenerateRandomResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GenerateRandomResponse -> GenerateRandomResponse -> Bool
$c/= :: GenerateRandomResponse -> GenerateRandomResponse -> Bool
== :: GenerateRandomResponse -> GenerateRandomResponse -> Bool
$c== :: GenerateRandomResponse -> GenerateRandomResponse -> Bool
Prelude.Eq, Int -> GenerateRandomResponse -> ShowS
[GenerateRandomResponse] -> ShowS
GenerateRandomResponse -> String
(Int -> GenerateRandomResponse -> ShowS)
-> (GenerateRandomResponse -> String)
-> ([GenerateRandomResponse] -> ShowS)
-> Show GenerateRandomResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GenerateRandomResponse] -> ShowS
$cshowList :: [GenerateRandomResponse] -> ShowS
show :: GenerateRandomResponse -> String
$cshow :: GenerateRandomResponse -> String
showsPrec :: Int -> GenerateRandomResponse -> ShowS
$cshowsPrec :: Int -> GenerateRandomResponse -> ShowS
Prelude.Show, (forall x. GenerateRandomResponse -> Rep GenerateRandomResponse x)
-> (forall x.
    Rep GenerateRandomResponse x -> GenerateRandomResponse)
-> Generic GenerateRandomResponse
forall x. Rep GenerateRandomResponse x -> GenerateRandomResponse
forall x. GenerateRandomResponse -> Rep GenerateRandomResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GenerateRandomResponse x -> GenerateRandomResponse
$cfrom :: forall x. GenerateRandomResponse -> Rep GenerateRandomResponse x
Prelude.Generic)

-- |
-- Create a value of 'GenerateRandomResponse' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'plaintext', 'generateRandomResponse_plaintext' - The random byte string. When you use the HTTP API or the Amazon Web
-- Services CLI, the value is Base64-encoded. Otherwise, it is not
-- Base64-encoded.--
-- -- /Note:/ This 'Lens' automatically encodes and decodes Base64 data.
-- -- The underlying isomorphism will encode to Base64 representation during
-- -- serialisation, and decode from Base64 representation during deserialisation.
-- -- This 'Lens' accepts and returns only raw unencoded data.
--
-- 'httpStatus', 'generateRandomResponse_httpStatus' - The response's http status code.
newGenerateRandomResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  GenerateRandomResponse
newGenerateRandomResponse :: Int -> GenerateRandomResponse
newGenerateRandomResponse Int
pHttpStatus_ =
  GenerateRandomResponse' :: Maybe (Sensitive Base64) -> Int -> GenerateRandomResponse
GenerateRandomResponse'
    { $sel:plaintext:GenerateRandomResponse' :: Maybe (Sensitive Base64)
plaintext =
        Maybe (Sensitive Base64)
forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:GenerateRandomResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | The random byte string. When you use the HTTP API or the Amazon Web
-- Services CLI, the value is Base64-encoded. Otherwise, it is not
-- Base64-encoded.--
-- -- /Note:/ This 'Lens' automatically encodes and decodes Base64 data.
-- -- The underlying isomorphism will encode to Base64 representation during
-- -- serialisation, and decode from Base64 representation during deserialisation.
-- -- This 'Lens' accepts and returns only raw unencoded data.
generateRandomResponse_plaintext :: Lens.Lens' GenerateRandomResponse (Prelude.Maybe Prelude.ByteString)
generateRandomResponse_plaintext :: (Maybe ByteString -> f (Maybe ByteString))
-> GenerateRandomResponse -> f GenerateRandomResponse
generateRandomResponse_plaintext = (GenerateRandomResponse -> Maybe (Sensitive Base64))
-> (GenerateRandomResponse
    -> Maybe (Sensitive Base64) -> GenerateRandomResponse)
-> Lens
     GenerateRandomResponse
     GenerateRandomResponse
     (Maybe (Sensitive Base64))
     (Maybe (Sensitive Base64))
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GenerateRandomResponse' {Maybe (Sensitive Base64)
plaintext :: Maybe (Sensitive Base64)
$sel:plaintext:GenerateRandomResponse' :: GenerateRandomResponse -> Maybe (Sensitive Base64)
plaintext} -> Maybe (Sensitive Base64)
plaintext) (\s :: GenerateRandomResponse
s@GenerateRandomResponse' {} Maybe (Sensitive Base64)
a -> GenerateRandomResponse
s {$sel:plaintext:GenerateRandomResponse' :: Maybe (Sensitive Base64)
plaintext = Maybe (Sensitive Base64)
a} :: GenerateRandomResponse) ((Maybe (Sensitive Base64) -> f (Maybe (Sensitive Base64)))
 -> GenerateRandomResponse -> f GenerateRandomResponse)
-> ((Maybe ByteString -> f (Maybe ByteString))
    -> Maybe (Sensitive Base64) -> f (Maybe (Sensitive Base64)))
-> (Maybe ByteString -> f (Maybe ByteString))
-> GenerateRandomResponse
-> f GenerateRandomResponse
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso (Sensitive Base64) (Sensitive Base64) ByteString ByteString
-> Iso
     (Maybe (Sensitive Base64))
     (Maybe (Sensitive Base64))
     (Maybe ByteString)
     (Maybe ByteString)
forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping (Exchange ByteString ByteString Base64 (Identity Base64)
-> Exchange
     ByteString
     ByteString
     (Sensitive Base64)
     (Identity (Sensitive Base64))
forall a. Iso' (Sensitive a) a
Core._Sensitive (Exchange ByteString ByteString Base64 (Identity Base64)
 -> Exchange
      ByteString
      ByteString
      (Sensitive Base64)
      (Identity (Sensitive Base64)))
-> (Exchange ByteString ByteString ByteString (Identity ByteString)
    -> Exchange ByteString ByteString Base64 (Identity Base64))
-> AnIso
     (Sensitive Base64) (Sensitive Base64) ByteString ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. Exchange ByteString ByteString ByteString (Identity ByteString)
-> Exchange ByteString ByteString Base64 (Identity Base64)
Iso' Base64 ByteString
Core._Base64)

-- | The response's http status code.
generateRandomResponse_httpStatus :: Lens.Lens' GenerateRandomResponse Prelude.Int
generateRandomResponse_httpStatus :: (Int -> f Int)
-> GenerateRandomResponse -> f GenerateRandomResponse
generateRandomResponse_httpStatus = (GenerateRandomResponse -> Int)
-> (GenerateRandomResponse -> Int -> GenerateRandomResponse)
-> Lens GenerateRandomResponse GenerateRandomResponse Int Int
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GenerateRandomResponse' {Int
httpStatus :: Int
$sel:httpStatus:GenerateRandomResponse' :: GenerateRandomResponse -> Int
httpStatus} -> Int
httpStatus) (\s :: GenerateRandomResponse
s@GenerateRandomResponse' {} Int
a -> GenerateRandomResponse
s {$sel:httpStatus:GenerateRandomResponse' :: Int
httpStatus = Int
a} :: GenerateRandomResponse)

instance Prelude.NFData GenerateRandomResponse