2017-01-31 6 views
0

私はいくつかの要素を持つ表を持っています。私は次のURLに基​​づいて特定の属性をユーザーがスキャンする、未認証(特定の属性をまだ認証されていないユーザーに戻したい)のファイングレイン・アクセス制御(表の特定の属性へのアクセスの制限)を使用しようとしていますAmazon DynamoDB:ファイングレイン・アクセス・コントロール特定の属性を持つスキャン

私は、次の役割を削除していた場合
{ 
"Version": "2012-10-17", 
"Statement": [ 
    { 
     "Sid": "LimitAccessToSpecificAttributes", 
     "Effect": "Allow", 
     "Action": [ 
      "dynamodb:GetItem", 
      "dynamodb:Query", 
      "dynamodb:BatchGetItem", 
      "dynamodb:Scan" 
     ], 
     "Resource": [ 
      "arn:aws:dynamodb:us-west-2:AccountID:table/MyTable" 
     ], 
     "Condition": { 
      "ForAllValues:StringEquals": { 
       "dynamodb:Attributes": [ 
        "startDate", 
        "endDate" 
       ] 
      }, 
      "StringEqualsIfExists": { 
       "dynamodb:Select": "SPECIFIC_ATTRIBUTES", 
       "dynamodb:ReturnValues": [ 
        "NONE", 
        "UPDATED_OLD", 
        "UPDATED_NEW" 
       ] 
      } 
     } 
    } 
] 
} 

は、それが動作します:

"Condition": { 
      "ForAllValues:StringEquals": { 
       "dynamodb:Attributes": [ 
        "startDate", 
        "endDate" 
       ] 
      }, 
      "StringEqualsIfExists": { 
       "dynamodb:Select": "SPECIFIC_ATTRIBUTES", 
       "dynamodb:ReturnValues": [ 
        "NONE", 
        "UPDATED_OLD", 
        "UPDATED_NEW" 
       ] 
      } 

問題は、私は私のコード(アンドロイド)を実行しているとき、私は次の例外を取得していますということです0

ユーザー:arn:aws:sts :: AccountID:想定されるロール/ Cognito_XXXUnauth_Role/CognitoIdentityCredentialsには実行が許可されていません:dynamodb:リソースのスキャン:arn:aws:dynamodb:us-east-1:AccountID:table/MyTableサービス:AmazonDynamoDB;ステータスコード:400;エラーコード:AccessDeniedException;

私は何が間違っているのか知りたいのですが、例外が発生します。 特定の属性を取得する他の方法はありますか?

私は、次のAndroidのコードを使用しています:

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(getApplicationContext(), 
         "identityPoolId", 
         Regions.US_EAST_1 
       ); 

AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(credentialsProvider); 

ScanRequest scanRequest = new ScanRequest(); 
scanRequest = scanRequest.withProjectionExpression("startDate, endDate"); 
scanRequest.setTableName("MyTable"); 

try { 
    ScanResult scanResult = ddb.scan(scanRequest); 
} catch (Exception ex) { 
    log(ex.getMessage()); 
} 

任意の助けをいただければ幸いです。

答えて

0

私はその理由を見つけました。ロールの属性には、ハッシュキーと範囲キーが含まれている必要があります。アトリビュートをハッシュキーとレンジキーに置き換えると、リサが答えたようにリージョンも変わってしまいました。

私は将来、誰もがそれを行うべきであるかを確認したい場合には、ここでの政策とAndroidのコードを掲載します:認証されていないユーザーのための

{ 
"Statement": [ 
    { 
     "Effect": "Allow", 
     "Action": [ 
      "dynamodb:DeleteItem", 
      "dynamodb:GetItem", 
      "dynamodb:PutItem", 
      "dynamodb:Scan", 
      "dynamodb:Query", 
      "dynamodb:UpdateItem", 
      "dynamodb:BatchWriteItem" 
     ], 
     "Resource": [ 
      "arn:aws:dynamodb:us-east-1:AcountID:table/MyTable", 
      "arn:aws:dynamodb:us-east-1:AcountID:table/MyTable/index/*" 
     ] 
    } 
]} 

ポリシー:

{ 
"Version": "2012-10-17", 
"Statement": [ 
    { 
     "Sid": "LimitAccessToSpecificAttributes", 
     "Effect": "Allow", 
     "Action": [ 
      "dynamodb:GetItem", 
      "dynamodb:Query", 
      "dynamodb:BatchGetItem", 
      "dynamodb:Scan" 
     ], 
     "Resource": [ 
      "arn:aws:dynamodb:us-east-1:AcountID:table/MyTable" 
     ], 
     "Condition": { 
      "ForAllValues:StringEquals": { 
       "dynamodb:Attributes": [ 
        "userId", 
        "startDate", 
        "endDate", 
        "Price" 
       ] 
      }, 
      "StringEqualsIfExists": { 
       "dynamodb:Select": "SPECIFIC_ATTRIBUTES" 
      } 
     } 
    } 
]} 

アンドロイドコードを認証されたユーザーと認証されていないユーザー(乱雑)のコードが含まれています。

public PaginatedScanList<Book> scan() { 

    PaginatedScanList<Book> result = null; 
    AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(_cognito.getCredentialsProvider()); 

    if (_cognito.getJWTToken() != null) { 
     //Authenticated (SignedIn) user flow 
     _cognito.credentialsProviderSetLogIn(); 
     mapper = new DynamoDBMapper(ddb); 

     DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); 
     result = mapper.scan(Book.class, scanExpression); 

     if (result != null) { 
      for (int i = 0; i < result.size(); i++) { 
       log("UserId: " + result.get(i).getUserId()); 
       log("Price: " + result.get(i).getPrice()); 
       log("startDate: " + result.get(i).getPrice()); 
       log("endDate: " + result.get(i).getPrice()); 
      } 
     } 
    } else { 
     //UnAuthenticated user flow 
     ScanRequest scanRequest = new ScanRequest(); 
     //Write attributes to be retreived. if Price is not exsits, items.get(i).get("Price").getS() will be null 
     //If Price is not exists in the policy, policy is null 
     scanRequest = scanRequest.withProjectionExpression("UserId, Price, startDate, endDate");//setter for projectionExpression 
     //scanRequest.setProjectionExpression("UserId, Price, startDate, endDate"); //not sure why this API exists. It does the same as withProjectionExpression. this is setter for projectionExpression 
     scanRequest.setSelect(Select.SPECIFIC_ATTRIBUTES); //Not usre this is needed. works well without it. 
     scanRequest.setTableName("MyTable"); 

     try { 
      ScanResult scanResult = ddb.scan(scanRequest); 
      final List<Map<String, AttributeValue>> items = scanResult.getItems(); 

      for (int i = 0; i < items.size(); i++) { 
       log("UserId: " + items.get(i).get("UserId").getS()); 
       log("Price: " + items.get(i).get("Price").getS()); 
       log("startDate: " + items.get(i).get("startDate").getS()); 
       log("endDate: " + items.get(i).get("endDate").getS()); 
      } 
     } catch (Exception ex) { 
      log(ex.getMessage()); 
     } 
    } 

    return result; 
} 
1

IAMポリシーを見ると、スキャン操作がリソース上で許可されているように見えます。arn:aws:dynamodb:us-west-2:AccountID:table/MyTable。

リソースはus-west-2用であることに注意してください。 しかし、us-east-1のリソースに対して操作を実行しようとしています。

0

IDプールに十分なDynamoDB権限(この場合はスキャンを実行する権限)があることを確認してください。 はここで認証されていない役割を作成し、それをDynamoDBのアクセスを許可することでCognitoを使用しての例です:次に http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Js.Summary.html

、他の人が言ったように、あなたはあなたに権限を付与されている同じ領域を指していることを確認してください。この場合、US_EAST_1に十分なCognitoおよびDDBアクセスがあることを確認してください。

関連する問題