Проблема соединения aws-java-sdk-v2 с потоком данных kinesis - PullRequest
0 голосов
/ 19 октября 2019

Я сделал этот код для соединения с локальным сервером и потоком данных Kinesis.

Если я запускаю свой код, accessKey, secretKey, token было взято из службы aws sts.

Но в результате отказано в доступе к потоку kinesis.

Я не знаю, почему он не может получить доступ к потоку kinesis с учетными данными sts, которые я взял.

Это мой pom.xml

 <properties> 
18         <java.version>1.8</java.version> 
19     </properties> 
20 

21     <dependencyManagement> 
22         <dependencies> 
23             <dependency> 
24                 <groupId>software.amazon.awssdk</groupId> 
25                 <artifactId>bom</artifactId> 
26                 <version>2.9.21</version> 
27                 <type>pom</type> 
28                 <scope>import</scope> 
29             </dependency> 
30         </dependencies> 
31     </dependencyManagement> 
32 

33     <dependencies> 
34         <dependency> 
35             <groupId>org.springframework.boot</groupId> 
36             <artifactId>spring-boot-starter-web</artifactId> 
37         </dependency> 
38 

39         <dependency> 
40             <groupId>org.springframework.boot</groupId> 
41             <artifactId>spring-boot-starter-test</artifactId> 
42             <scope>test</scope> 
43         </dependency> 
44 

45         <dependency> 
46             <groupId>software.amazon.awssdk</groupId> 
47             <artifactId>aws-sdk-java</artifactId> 
48             <version>2.9.21</version> 
49         </dependency> 
50 

51         <dependency> 
52             <groupId>com.amazonaws</groupId> 
53             <artifactId>amazon-kinesis-producer</artifactId> 
54             <version>0.13.1</version> 
55         </dependency> 
56 

57     </dependencies> 
58 

59     <build> 
60         <plugins> 
61             <plugin> 
62                 <groupId>org.springframework.boot</groupId> 
63                 <artifactId>spring-boot-maven-plugin</artifactId> 
64             </plugin> 
65         </plugins> 
66     </build> 

Это controller.java локального сервера

@RestController 
28 @RequestMapping(value="/logs") 
29 public class LogController { 
30 

31     final static Logger logger = LoggerFactory.getLogger(LogController.class); 
32 

33     // kinesis variables 
34     @Value("${aws_kinesis_stream_name}") 
35     private String streamName; 
36     @Value("${aws_kinesis_partition_key}") 
37     private String partitionkey; 
38 

39     @RequestMapping(value="/{type}/{num}", method=RequestMethod.GET) 
40     public void createLog(HttpServletResponse response, @PathVariable int num, @PathVariable String type) throws IOException { 
41          
42          
43         // STS configuration 
44         StsClient stsClient = StsClient.create(); 
45 

46         GetSessionTokenRequest tokenRequest = GetSessionTokenRequest.builder().durationSeconds(7200).build(); 
47 

48         GetSessionTokenResponse tokenResponse = stsClient.getSessionToken(tokenRequest); 
49 

50         Credentials sessionCredentials = tokenResponse.credentials(); 
51 

52         // AwsSessionCredentials awsSessionCredentials = AwsSessionCredentials.create( 
53         //     sessionCredentials.accessKeyId(),  
54         //     sessionCredentials.secretAccessKey(),  
55         //     sessionCredentials.sessionToken()); 
56 

57         BasicSessionCredentials basicSessionCredentials = new BasicSessionCredentials( 
58             sessionCredentials.accessKeyId(),  
59             sessionCredentials.secretAccessKey(),  
60             sessionCredentials.sessionToken()); 
61          
62          
63 

64         // kinesis configuration 
65         KinesisProducerConfiguration config = new KinesisProducerConfiguration() 
66             .setRecordMaxBufferedTime(3000) 
67             .setMaxConnections(1) 
68             .setRequestTimeout(60000) 
69             .setRegion("ap-northeast-2") 
70             .setCredentialsProvider(new AWSStaticCredentialsProvider(basicSessionCredentials)); 
71          
72         final KinesisProducer kinesis = new KinesisProducer(config); 
73 

74         // This test code is not checked if recording was success or not 
75         // To check up if success to record, need to add code in next link.<https://docs.aws.amazon.com/ko_kr/streams/latest/dev/kinesis-kpl-writing.html> 
76         // 일단 스킵~ 
77         for (int i=1 ; i <= num ; i++)  { 
78             String caseName = ""; 
79             switch (type) { 
80                 case "debug": 
81                     logger.debug("created debug log : " + i); 
82                     caseName = "debug" + i + "\n"; 
83                 break; 
84                 case "info":  
85                     logger.info("created info log : " + i);  
86                     caseName = "info" + i + "\n"; 
87                 break; 
88                 case "error":  
89                     logger.error("created error log : " + i);  
90                     caseName = "error" + i + "\n"; 
91                 break; 
92                 case "trace":  
93                     logger.trace("created trace log : " + i);  
94                     caseName = "trace" + i + "\n"; 
95                 break; 
96                 case "warn":  
97                     logger.warn("created warn log : " + i);  
98                     caseName = "warn" + i + "\n"; 
99                 break; 
100             } 
101             ByteBuffer data = ByteBuffer.wrap(caseName.getBytes("UTF-8")); 
102             // byte[] bytes = new byte[caseName.length()]; 
103             // data.position(0); 
104             // data.get(bytes); 
105             // String s = new String(bytes); 
106             // System.out.println("data: " + s); 
107             kinesis.addUserRecord(streamName, partitionkey, data); 
108         } 
109         response.getWriter().println("log created"); 
110         response.getWriter().println("type : " + type); 
111         response.getWriter().println("amount : " + num); 
112     } 
113 

114     public static void defaultLog(String type, int num) { 
115         for (int i=1 ; i <= num ; i++)  { 
116             switch (type) { 
117                 case "debug": logger.debug("created debug log : " + i); break; 
118                 case "info": logger.info("created info log : " + i); break; 
119                 case "error": logger.error("created error log : " + i); break; 
120                 case "trace": logger.trace("created trace log : " + i); break; 
121                 case "warn": logger.warn("created warn log : " + i); break; 
122             } 
123         } 
124     } 
125 } 

Журнал ошибок приведен ниже ..

2019-10-20 11:49:41.511  INFO 12356 --- [kpl-daemon-0002] c.a.s.k.producer.LogInputStreamReader    : [2019-10-20 11:49:34.370220] [0x00001294] [info]    [shard_map.cc:79] Updating shard map for stream "access-test"
2019-10-20 11:49:41.567  INFO 12356 --- [kpl-daemon-0002] c.a.s.k.producer.LogInputStreamReader    : [2019-10-20 11:49:34.409181] [0x00002b1c] [error]   [shard_map.cc:152] Shard map update for stream "access-test" failed. Code: AccessDeniedException Message: User: arn:aws:iam::123456789012:user/jk-kinesis-sts-test is not authorized to perform: kinesis:DescribeStream on resource: arn:aws:kinesis:ap-northeast-2:123456789012:stream/access-test; retrying in 5062 ms
2019-10-20 11:49:41.569  INFO 12356 --- [kpl-daemon-0002] c.a.s.k.producer.LogInputStreamReader    : [2019-10-20 11:49:39.471916] [0x00001294] [info]    [shard_map.cc:79] Updating shard map for stream "access-test"
2019-10-20 11:49:41.569  INFO 12356 --- [kpl-daemon-0002] c.a.s.k.producer.LogInputStreamReader    : [2019-10-20 11:49:39.503323] [0x000009dc] [error]   [shard_map.cc:152] Shard map update for stream "access-test" failed. Code: AccessDeniedException Message: User: arn:aws:iam::123456789012:user/jk-kinesis-sts-test is not authorized to perform: kinesis:DescribeStream on resource: arn:aws:kinesis:ap-northeast-2:123456789012:stream/access-test; retrying in 7593 ms

Весь мой код находится по этой ссылке,https://github.com/sizzflyer/ecs-kinesis-logtest

...