Git Product home page Git Product logo

Comments (5)

dchakrav-github avatar dchakrav-github commented on August 15, 2024 1

With Guard 2.0 you can select keys for a map that match a regular expression. The above rule in 2.0 is as follows

AWS::IAM::Role Properties {
    Policies.*.PolicyDocument.Statement.*.Resource[ KEYS == /.+Sub/ ].* == "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"
}

It would work for example given. However, it isn't correct as written. For AWS::IAM::Role the Resource attribute is multi-valued or in other words is an array of values. It is possible to have any of the following specified in a CFN template

"Resource": [
  {
      "Fn::Sub": "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"
  },
  "aws:arn...."
]

or

"Resource": "aws:arn...."

or

"Resource": ["aws:arn...", "aws:arn"]

Here you are only interested in targeting those that contain a Fn::Sub reference. Here is the way to specify this in Guard 2.0

AWS::IAM::Role Properties {
    # 
    #  Select all Resource elements that have 'Fn::Sub'
    # 
    let resources = Policies.*.PolicyDocument.Statement.*.Resource[ 'Fn::Sub' EXISTS ]
    when %resources !EMPTY {
        %resources.* == "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"
    }
}

How do we know if the rule really worked? You can unit test this rule with Guard 2.0. Here is a sample test suite

- input:
    Resources:
      role:
          Type: AWS::IAM::Role
          Properties: 
             Policies:
                 - PolicyDocument:
                     Statement:
                         - Resource: "aws:arn"
  expectations:
    rules:
        default: SKIP
- input:
    Resources:
      role:
          Type: AWS::IAM::Role
          Properties: 
             Policies:
                 - PolicyDocument:
                     Statement:
                         - Resource:
                             "Fn::Sub": "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"
  expectations:
    rules:
        default: PASS
- input:
    Resources:
      role:
          Type: AWS::IAM::Role
          Properties: 
             Policies:
                 - PolicyDocument:
                     Statement:
                         - Resource:
                             - "Fn::Sub": "fail"
                             - "*"
  expectations:
    rules:
        default: FAIL
- input:
    Resources:
      role:
          Type: AWS::IAM::Role
          Properties: 
             Policies:
                 - PolicyDocument:
                     Statement:
                         - Resource:
                             - "aws:arn"
                             - "*"
  expectations:
    rules:
        default: SKIP

We are internally tracking a feature where this expression would simplify to

AWS::IAM::Role Properties {
    Policies.*.PolicyDocument.Statement.*.Resource[ 'Fn::Sub' EXISTS ].* ==  "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"   
}

where it SKIPs the results if there are no such Resources present. For now, we need to use the when !EMPTY condition check.

from cloudformation-guard.

z-sourcecode avatar z-sourcecode commented on August 15, 2024

+1 Regex support will be great for complicated conditions

from cloudformation-guard.

johnttompkins avatar johnttompkins commented on August 15, 2024

We are providing enhanced property access feature in the next release of the tooling, as detailed in #115. We hope to encourage users to do these checks using the KEYS operator to avoid using regular expression inside of json. So instead of

AWS::IAM::Role Policies.*.PolicyDocument.Statement.*.Resource == {/.+Sub/: "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"}

one could write in the new version of the rules language

AWS::IAM::Role Policies.*.PolicyDocument.Statement.*.Resource KEYS == /.+Sub/
AWS::IAM::Role Policies.*.PolicyDocument.Statement.*.Resource.* ==  "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"
}

As of now, we have no plan to support regular expression for json keys as listed above.

from cloudformation-guard.

dchakrav-github avatar dchakrav-github commented on August 15, 2024

Attaching samples with tests
policies-examples.zip

from cloudformation-guard.

dchakrav-github avatar dchakrav-github commented on August 15, 2024

Resolving, please re-open if you needed any more support on this. thank you

from cloudformation-guard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.