0

我们目前正在使用 API 密钥来保护对我们 API 网关的访问。但是,我们正在迁移到具有访问/密钥的 IAM 模型。我了解 swagger 不允许我们这样做(我们目前在 swagger 中设置了 api_key 以启用 API Key 身份验证)。

我创建了各种操作所需的策略:

  SvcAccountPolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      ManagedPolicyName: !Sub 'iam-${EnvTag}'
      PolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Action:
              - 'execute-api:Invoke'
            Resource:
              - !Sub 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${SomeApi}/*/GET/*'
              - !Sub 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${SomeApi}/*/POST/*'
              - !Sub 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${SomeApi}/*/PUT/*'
              - !Sub 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${SomeApi}/*/DELETE/*'
      Users:
        - !Ref userSvcAcct

我的 lambda 函数如下所示。我对云的形成仍然很陌生,并且希望我能做些什么来为此添加一个 lambda 授权者(我相信它将在 Events->ApiPost/Get 等部分),这将允许我使用秘密/访问密钥。

  FtpUserMgmtLambda:
    Type: AWS::Serverless::Function
    Properties:
      Description: Lambda handler function for FTP user management
      Handler: 'handler.UserManagementHandler::handleRequest'
      Runtime: java8
      MemorySize: 512
      Timeout: 300
      FunctionName: !Ref LambdaFunctionName
      Role: !GetAtt UserMgmtLambdaRole.Arn
      CodeUri:
        Bucket: !Ref LambdaCodeS3Bucket
        Key: !Ref LambdaCodeFileName
      VpcConfig:
        SomeConfig stuff here
      Environment:
        Variables:
          dbPort: !Ref UserStoreDbPort
          dbUser: !Ref UserStoreDbUserId
          dbName: !Ref UserStoreDbName
          environment: !Ref EnvTag
          basepath: 'somepath'
      Events:
        ApiPost:
          Type: Api
          Properties:
            RestApiId: !Ref SomeApi
            Path: /path
            Method: POST
            Auth: <<Dont know what to do here! HELP>>
        ApiGet:
          Type: Api
          Properties:
            RestApiId: !Ref SomeApi
            Path: /path
            Method: GET
            Auth: *<<Dont know what to do here! HELP>>*
      Tags:
        Name: !Ref LambdaFunctionName
        function: lambda function that manages ftp users
4

1 回答 1

0

Fixed this through Swagger. Example code as under:

---
swagger: "2.0"
info:
  version: "2017-10-17T17:47:44Z"
  title: "User-Mgt-API"
basePath: "/${environment}"
schemes:
  - "https"
paths:
  /ftpuser:
    post:
      x-amazon-apigateway-auth:
        type: aws_iam
      produces:
        - "application/json"
      responses:
        200:
          description: "When create user request successful"
          schema:
            $ref: "#/definitions/Empty"
        400:
          description: "When API vallidation error happens"
          schema:
            $ref: "#/definitions/Empty"
      x-amazon-apigateway-integration:
        responses:
          default:
               statusCode: "200"
        uri:
          Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${FtpUserMgmtLambda.Arn}/invocations
        passthroughBehavior: "when_no_match"
        httpMethod: "POST"
        contentHandling: "CONVERT_TO_TEXT"
        type: "aws_proxy"
 definitions:
  Empty:
    type: "object"
    title: "Empty Schema"

Then in the cloudformation, added the following to the serverless API definition to process the swagger file. Of course,

FtpUserMgmtApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: !Ref ApiName
      StageName: !Ref ApiDeploymentStageName
      DefinitionBody:
        Fn::Transform:
          Name: AWS::Include
          Parameters:
            Location: !Sub s3://${swaggerS3Location}

Hope this helps. There is also an example on the web to use x-amazon-apigateway-any-method using which I derived the above. That link is here.

于 2019-02-12T16:31:32.873 回答