{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# 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.Types.GrantConstraints
-- 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)
module Amazonka.KMS.Types.GrantConstraints where

import qualified Amazonka.Core as Core
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude

-- | Use this structure to allow
-- <https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#cryptographic-operations cryptographic operations>
-- in the grant only when the operation request includes the specified
-- <https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#encrypt_context encryption context>.
--
-- KMS applies the grant constraints only to cryptographic operations that
-- support an encryption context, that is, all cryptographic operations
-- with a
-- <https://docs.aws.amazon.com/kms/latest/developerguide/symm-asymm-concepts.html#symmetric-cmks symmetric KMS key>.
-- Grant constraints are not applied to operations that do not support an
-- encryption context, such as cryptographic operations with asymmetric KMS
-- keys and management operations, such as DescribeKey or RetireGrant.
--
-- In a cryptographic operation, the encryption context in the decryption
-- operation must be an exact, case-sensitive match for the keys and values
-- in the encryption context of the encryption operation. Only the order of
-- the pairs can vary.
--
-- However, in a grant constraint, the key in each key-value pair is not
-- case sensitive, but the value is case sensitive.
--
-- To avoid confusion, do not use multiple encryption context pairs that
-- differ only by case. To require a fully case-sensitive encryption
-- context, use the @kms:EncryptionContext:@ and
-- @kms:EncryptionContextKeys@ conditions in an IAM or key policy. For
-- details, see
-- <https://docs.aws.amazon.com/kms/latest/developerguide/policy-conditions.html#conditions-kms-encryption-context kms:EncryptionContext:>
-- in the //Key Management Service Developer Guide// .
--
-- /See:/ 'newGrantConstraints' smart constructor.
data GrantConstraints = GrantConstraints'
  { -- | A list of key-value pairs that must match the encryption context in the
    -- <https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#cryptographic-operations cryptographic operation>
    -- request. The grant allows the operation only when the encryption context
    -- in the request is the same as the encryption context specified in this
    -- constraint.
    GrantConstraints -> Maybe (HashMap Text Text)
encryptionContextEquals :: Prelude.Maybe (Prelude.HashMap Prelude.Text Prelude.Text),
    -- | A list of key-value pairs that must be included in the encryption
    -- context of the
    -- <https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#cryptographic-operations cryptographic operation>
    -- request. The grant allows the cryptographic operation only when the
    -- encryption context in the request includes the key-value pairs specified
    -- in this constraint, although it can include additional key-value pairs.
    GrantConstraints -> Maybe (HashMap Text Text)
encryptionContextSubset :: Prelude.Maybe (Prelude.HashMap Prelude.Text Prelude.Text)
  }
  deriving (GrantConstraints -> GrantConstraints -> Bool
(GrantConstraints -> GrantConstraints -> Bool)
-> (GrantConstraints -> GrantConstraints -> Bool)
-> Eq GrantConstraints
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GrantConstraints -> GrantConstraints -> Bool
$c/= :: GrantConstraints -> GrantConstraints -> Bool
== :: GrantConstraints -> GrantConstraints -> Bool
$c== :: GrantConstraints -> GrantConstraints -> Bool
Prelude.Eq, ReadPrec [GrantConstraints]
ReadPrec GrantConstraints
Int -> ReadS GrantConstraints
ReadS [GrantConstraints]
(Int -> ReadS GrantConstraints)
-> ReadS [GrantConstraints]
-> ReadPrec GrantConstraints
-> ReadPrec [GrantConstraints]
-> Read GrantConstraints
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GrantConstraints]
$creadListPrec :: ReadPrec [GrantConstraints]
readPrec :: ReadPrec GrantConstraints
$creadPrec :: ReadPrec GrantConstraints
readList :: ReadS [GrantConstraints]
$creadList :: ReadS [GrantConstraints]
readsPrec :: Int -> ReadS GrantConstraints
$creadsPrec :: Int -> ReadS GrantConstraints
Prelude.Read, Int -> GrantConstraints -> ShowS
[GrantConstraints] -> ShowS
GrantConstraints -> String
(Int -> GrantConstraints -> ShowS)
-> (GrantConstraints -> String)
-> ([GrantConstraints] -> ShowS)
-> Show GrantConstraints
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GrantConstraints] -> ShowS
$cshowList :: [GrantConstraints] -> ShowS
show :: GrantConstraints -> String
$cshow :: GrantConstraints -> String
showsPrec :: Int -> GrantConstraints -> ShowS
$cshowsPrec :: Int -> GrantConstraints -> ShowS
Prelude.Show, (forall x. GrantConstraints -> Rep GrantConstraints x)
-> (forall x. Rep GrantConstraints x -> GrantConstraints)
-> Generic GrantConstraints
forall x. Rep GrantConstraints x -> GrantConstraints
forall x. GrantConstraints -> Rep GrantConstraints x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GrantConstraints x -> GrantConstraints
$cfrom :: forall x. GrantConstraints -> Rep GrantConstraints x
Prelude.Generic)

-- |
-- Create a value of 'GrantConstraints' 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:
--
-- 'encryptionContextEquals', 'grantConstraints_encryptionContextEquals' - A list of key-value pairs that must match the encryption context in the
-- <https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#cryptographic-operations cryptographic operation>
-- request. The grant allows the operation only when the encryption context
-- in the request is the same as the encryption context specified in this
-- constraint.
--
-- 'encryptionContextSubset', 'grantConstraints_encryptionContextSubset' - A list of key-value pairs that must be included in the encryption
-- context of the
-- <https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#cryptographic-operations cryptographic operation>
-- request. The grant allows the cryptographic operation only when the
-- encryption context in the request includes the key-value pairs specified
-- in this constraint, although it can include additional key-value pairs.
newGrantConstraints ::
  GrantConstraints
newGrantConstraints :: GrantConstraints
newGrantConstraints =
  GrantConstraints' :: Maybe (HashMap Text Text)
-> Maybe (HashMap Text Text) -> GrantConstraints
GrantConstraints'
    { $sel:encryptionContextEquals:GrantConstraints' :: Maybe (HashMap Text Text)
encryptionContextEquals =
        Maybe (HashMap Text Text)
forall a. Maybe a
Prelude.Nothing,
      $sel:encryptionContextSubset:GrantConstraints' :: Maybe (HashMap Text Text)
encryptionContextSubset = Maybe (HashMap Text Text)
forall a. Maybe a
Prelude.Nothing
    }

-- | A list of key-value pairs that must match the encryption context in the
-- <https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#cryptographic-operations cryptographic operation>
-- request. The grant allows the operation only when the encryption context
-- in the request is the same as the encryption context specified in this
-- constraint.
grantConstraints_encryptionContextEquals :: Lens.Lens' GrantConstraints (Prelude.Maybe (Prelude.HashMap Prelude.Text Prelude.Text))
grantConstraints_encryptionContextEquals :: (Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
-> GrantConstraints -> f GrantConstraints
grantConstraints_encryptionContextEquals = (GrantConstraints -> Maybe (HashMap Text Text))
-> (GrantConstraints
    -> Maybe (HashMap Text Text) -> GrantConstraints)
-> Lens
     GrantConstraints
     GrantConstraints
     (Maybe (HashMap Text Text))
     (Maybe (HashMap Text Text))
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GrantConstraints' {Maybe (HashMap Text Text)
encryptionContextEquals :: Maybe (HashMap Text Text)
$sel:encryptionContextEquals:GrantConstraints' :: GrantConstraints -> Maybe (HashMap Text Text)
encryptionContextEquals} -> Maybe (HashMap Text Text)
encryptionContextEquals) (\s :: GrantConstraints
s@GrantConstraints' {} Maybe (HashMap Text Text)
a -> GrantConstraints
s {$sel:encryptionContextEquals:GrantConstraints' :: Maybe (HashMap Text Text)
encryptionContextEquals = Maybe (HashMap Text Text)
a} :: GrantConstraints) ((Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
 -> GrantConstraints -> f GrantConstraints)
-> ((Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
    -> Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
-> (Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
-> GrantConstraints
-> f GrantConstraints
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso
  (HashMap Text Text)
  (HashMap Text Text)
  (HashMap Text Text)
  (HashMap Text Text)
-> Iso
     (Maybe (HashMap Text Text))
     (Maybe (HashMap Text Text))
     (Maybe (HashMap Text Text))
     (Maybe (HashMap Text Text))
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 AnIso
  (HashMap Text Text)
  (HashMap Text Text)
  (HashMap Text Text)
  (HashMap Text Text)
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | A list of key-value pairs that must be included in the encryption
-- context of the
-- <https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#cryptographic-operations cryptographic operation>
-- request. The grant allows the cryptographic operation only when the
-- encryption context in the request includes the key-value pairs specified
-- in this constraint, although it can include additional key-value pairs.
grantConstraints_encryptionContextSubset :: Lens.Lens' GrantConstraints (Prelude.Maybe (Prelude.HashMap Prelude.Text Prelude.Text))
grantConstraints_encryptionContextSubset :: (Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
-> GrantConstraints -> f GrantConstraints
grantConstraints_encryptionContextSubset = (GrantConstraints -> Maybe (HashMap Text Text))
-> (GrantConstraints
    -> Maybe (HashMap Text Text) -> GrantConstraints)
-> Lens
     GrantConstraints
     GrantConstraints
     (Maybe (HashMap Text Text))
     (Maybe (HashMap Text Text))
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GrantConstraints' {Maybe (HashMap Text Text)
encryptionContextSubset :: Maybe (HashMap Text Text)
$sel:encryptionContextSubset:GrantConstraints' :: GrantConstraints -> Maybe (HashMap Text Text)
encryptionContextSubset} -> Maybe (HashMap Text Text)
encryptionContextSubset) (\s :: GrantConstraints
s@GrantConstraints' {} Maybe (HashMap Text Text)
a -> GrantConstraints
s {$sel:encryptionContextSubset:GrantConstraints' :: Maybe (HashMap Text Text)
encryptionContextSubset = Maybe (HashMap Text Text)
a} :: GrantConstraints) ((Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
 -> GrantConstraints -> f GrantConstraints)
-> ((Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
    -> Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
-> (Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
-> GrantConstraints
-> f GrantConstraints
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso
  (HashMap Text Text)
  (HashMap Text Text)
  (HashMap Text Text)
  (HashMap Text Text)
-> Iso
     (Maybe (HashMap Text Text))
     (Maybe (HashMap Text Text))
     (Maybe (HashMap Text Text))
     (Maybe (HashMap Text Text))
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 AnIso
  (HashMap Text Text)
  (HashMap Text Text)
  (HashMap Text Text)
  (HashMap Text Text)
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

instance Core.FromJSON GrantConstraints where
  parseJSON :: Value -> Parser GrantConstraints
parseJSON =
    String
-> (Object -> Parser GrantConstraints)
-> Value
-> Parser GrantConstraints
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"GrantConstraints"
      ( \Object
x ->
          Maybe (HashMap Text Text)
-> Maybe (HashMap Text Text) -> GrantConstraints
GrantConstraints'
            (Maybe (HashMap Text Text)
 -> Maybe (HashMap Text Text) -> GrantConstraints)
-> Parser (Maybe (HashMap Text Text))
-> Parser (Maybe (HashMap Text Text) -> GrantConstraints)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> ( Object
x Object -> Text -> Parser (Maybe (Maybe (HashMap Text Text)))
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"EncryptionContextEquals"
                            Parser (Maybe (Maybe (HashMap Text Text)))
-> Maybe (HashMap Text Text) -> Parser (Maybe (HashMap Text Text))
forall a. Parser (Maybe a) -> a -> Parser a
Core..!= Maybe (HashMap Text Text)
forall a. Monoid a => a
Prelude.mempty
                        )
            Parser (Maybe (HashMap Text Text) -> GrantConstraints)
-> Parser (Maybe (HashMap Text Text)) -> Parser GrantConstraints
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> ( Object
x Object -> Text -> Parser (Maybe (Maybe (HashMap Text Text)))
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"EncryptionContextSubset"
                            Parser (Maybe (Maybe (HashMap Text Text)))
-> Maybe (HashMap Text Text) -> Parser (Maybe (HashMap Text Text))
forall a. Parser (Maybe a) -> a -> Parser a
Core..!= Maybe (HashMap Text Text)
forall a. Monoid a => a
Prelude.mempty
                        )
      )

instance Prelude.Hashable GrantConstraints

instance Prelude.NFData GrantConstraints

instance Core.ToJSON GrantConstraints where
  toJSON :: GrantConstraints -> Value
toJSON GrantConstraints' {Maybe (HashMap Text Text)
encryptionContextSubset :: Maybe (HashMap Text Text)
encryptionContextEquals :: Maybe (HashMap Text Text)
$sel:encryptionContextSubset:GrantConstraints' :: GrantConstraints -> Maybe (HashMap Text Text)
$sel:encryptionContextEquals:GrantConstraints' :: GrantConstraints -> Maybe (HashMap Text Text)
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"EncryptionContextEquals" Text -> HashMap Text Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (HashMap Text Text -> Pair)
-> Maybe (HashMap Text Text) -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (HashMap Text Text)
encryptionContextEquals,
            (Text
"EncryptionContextSubset" Text -> HashMap Text Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (HashMap Text Text -> Pair)
-> Maybe (HashMap Text Text) -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (HashMap Text Text)
encryptionContextSubset
          ]
      )