{-# 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-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Returns a random byte string that is cryptographically secure.
--
-- You must use the @NumberOfBytes@ parameter to specify the length of the
-- random byte string. There is no default value for string length.
--
-- By default, the random byte string is generated in KMS. To generate the
-- byte string in the CloudHSM cluster associated with an CloudHSM key
-- store, use the @CustomKeyStoreId@ parameter.
--
-- 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>.
--
-- __Cross-account use__: Not applicable. @GenerateRandom@ does not use any
-- account-specific resources, such as KMS keys.
--
-- __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_customKeyStoreId,
    generateRandom_numberOfBytes,

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

    -- * Response Lenses
    generateRandomResponse_plaintext,
    generateRandomResponse_httpStatus,
  )
where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import Amazonka.KMS.Types
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'
  { -- | Generates the random byte string in the CloudHSM cluster that is
    -- associated with the specified CloudHSM key store. To find the ID of a
    -- custom key store, use the DescribeCustomKeyStores operation.
    --
    -- External key store IDs are not valid for this parameter. If you specify
    -- the ID of an external key store, @GenerateRandom@ throws an
    -- @UnsupportedOperationException@.
    GenerateRandom -> Maybe Text
customKeyStoreId :: Prelude.Maybe Prelude.Text,
    -- | The length of the random byte string. This parameter is required.
    GenerateRandom -> Maybe Natural
numberOfBytes :: Prelude.Maybe Prelude.Natural
  }
  deriving (GenerateRandom -> GenerateRandom -> Bool
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]
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
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. 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:
--
-- 'customKeyStoreId', 'generateRandom_customKeyStoreId' - Generates the random byte string in the CloudHSM cluster that is
-- associated with the specified CloudHSM key store. To find the ID of a
-- custom key store, use the DescribeCustomKeyStores operation.
--
-- External key store IDs are not valid for this parameter. If you specify
-- the ID of an external key store, @GenerateRandom@ throws an
-- @UnsupportedOperationException@.
--
-- 'numberOfBytes', 'generateRandom_numberOfBytes' - The length of the random byte string. This parameter is required.
newGenerateRandom ::
  GenerateRandom
newGenerateRandom :: GenerateRandom
newGenerateRandom =
  GenerateRandom'
    { $sel:customKeyStoreId:GenerateRandom' :: Maybe Text
customKeyStoreId = forall a. Maybe a
Prelude.Nothing,
      $sel:numberOfBytes:GenerateRandom' :: Maybe Natural
numberOfBytes = forall a. Maybe a
Prelude.Nothing
    }

-- | Generates the random byte string in the CloudHSM cluster that is
-- associated with the specified CloudHSM key store. To find the ID of a
-- custom key store, use the DescribeCustomKeyStores operation.
--
-- External key store IDs are not valid for this parameter. If you specify
-- the ID of an external key store, @GenerateRandom@ throws an
-- @UnsupportedOperationException@.
generateRandom_customKeyStoreId :: Lens.Lens' GenerateRandom (Prelude.Maybe Prelude.Text)
generateRandom_customKeyStoreId :: Lens' GenerateRandom (Maybe Text)
generateRandom_customKeyStoreId = 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)

-- | The length of the random byte string. This parameter is required.
generateRandom_numberOfBytes :: Lens.Lens' GenerateRandom (Prelude.Maybe Prelude.Natural)
generateRandom_numberOfBytes :: Lens' GenerateRandom (Maybe Natural)
generateRandom_numberOfBytes = 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)

instance Core.AWSRequest GenerateRandom where
  type
    AWSResponse GenerateRandom =
      GenerateRandomResponse
  request :: (Service -> Service) -> GenerateRandom -> Request GenerateRandom
request Service -> Service
overrides =
    forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON (Service -> Service
overrides Service
defaultService)
  response :: forall (m :: * -> *).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy GenerateRandom
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse GenerateRandom)))
response =
    forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> Object -> Either String (AWSResponse a))
-> (ByteStringLazy -> IO ByteStringLazy)
-> 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'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Either String (Maybe a)
Data..?> Key
"Plaintext")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (forall (f :: * -> *) a. Applicative f => a -> f a
Prelude.pure (forall a. Enum a => a -> Int
Prelude.fromEnum Int
s))
      )

instance Prelude.Hashable GenerateRandom where
  hashWithSalt :: Int -> GenerateRandom -> Int
hashWithSalt Int
_salt GenerateRandom' {Maybe Natural
Maybe Text
numberOfBytes :: Maybe Natural
customKeyStoreId :: Maybe Text
$sel:numberOfBytes:GenerateRandom' :: GenerateRandom -> Maybe Natural
$sel:customKeyStoreId:GenerateRandom' :: GenerateRandom -> Maybe Text
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Text
customKeyStoreId
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Natural
numberOfBytes

instance Prelude.NFData GenerateRandom where
  rnf :: GenerateRandom -> ()
rnf GenerateRandom' {Maybe Natural
Maybe Text
numberOfBytes :: Maybe Natural
customKeyStoreId :: Maybe Text
$sel:numberOfBytes:GenerateRandom' :: GenerateRandom -> Maybe Natural
$sel:customKeyStoreId:GenerateRandom' :: GenerateRandom -> Maybe Text
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
customKeyStoreId
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Natural
numberOfBytes

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

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

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

instance Data.ToQuery GenerateRandom where
  toQuery :: GenerateRandom -> QueryString
toQuery = forall a b. a -> b -> a
Prelude.const 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 (Data.Sensitive Data.Base64),
    -- | The response's http status code.
    GenerateRandomResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (GenerateRandomResponse -> GenerateRandomResponse -> Bool
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
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. 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'
    { $sel:plaintext:GenerateRandomResponse' :: Maybe (Sensitive Base64)
plaintext =
        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 :: Lens' GenerateRandomResponse (Maybe ByteString)
generateRandomResponse_plaintext = 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) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. 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 (forall a. Iso' (Sensitive a) a
Data._Sensitive forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. Iso' Base64 ByteString
Data._Base64)

-- | The response's http status code.
generateRandomResponse_httpStatus :: Lens.Lens' GenerateRandomResponse Prelude.Int
generateRandomResponse_httpStatus :: Lens' GenerateRandomResponse Int
generateRandomResponse_httpStatus = 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 where
  rnf :: GenerateRandomResponse -> ()
rnf GenerateRandomResponse' {Int
Maybe (Sensitive Base64)
httpStatus :: Int
plaintext :: Maybe (Sensitive Base64)
$sel:httpStatus:GenerateRandomResponse' :: GenerateRandomResponse -> Int
$sel:plaintext:GenerateRandomResponse' :: GenerateRandomResponse -> Maybe (Sensitive Base64)
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe (Sensitive Base64)
plaintext
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Int
httpStatus