embulk-input-cloudwatch_logs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ package org.embulk.input.cloudwatch_logs;
2
+
3
+ import java.util.List;
4
+
5
+ import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
6
+ import com.amazonaws.services.logs.AWSLogsClientBuilder;
7
+ import com.amazonaws.services.logs.AWSLogs;
8
+ import com.google.common.base.Optional;
9
+
10
+ import org.embulk.config.Config;
11
+ import org.embulk.config.ConfigDefault;
12
+ import org.embulk.config.ConfigDiff;
13
+ import org.embulk.config.ConfigException;
14
+ import org.embulk.config.ConfigSource;
15
+ import org.embulk.config.Task;
16
+ import org.embulk.config.TaskReport;
17
+ import org.embulk.config.TaskSource;
18
+ import org.embulk.spi.Exec;
19
+ import org.embulk.spi.InputPlugin;
20
+ import org.embulk.spi.PageOutput;
21
+ import org.embulk.spi.Schema;
22
+ import org.embulk.spi.SchemaConfig;
23
+
24
+ public class CloudwatchLogsInputPlugin
25
+ extends AbstractCloudwatchLogsInputPlugin
26
+ {
27
+ public interface CloudWatchLogsPluginTask
28
+ extends PluginTask
29
+ {
30
+ @Config("region")
31
+ @ConfigDefault("null")
32
+ Optional<String> getRegion();
33
+ }
34
+
35
+ @Override
36
+ protected Class<? extends PluginTask> getTaskClass()
37
+ {
38
+ return CloudWatchLogsPluginTask.class;
39
+ }
40
+
41
+ @Override
42
+ protected AWSLogs newLogsClient(PluginTask task)
43
+ {
44
+ CloudWatchLogsPluginTask t = (CloudWatchLogsPluginTask) task;
45
+ Optional<String> region = t.getRegion();
46
+ AWSLogsClientBuilder builder = super.defaultLogsClientBuilder(t);
47
+
48
+ if (region.isPresent()) {
49
+ builder.setRegion(region.get());
50
+ }
51
+ else {
52
+ throw new ConfigException("region is required");
53
+ }
54
+
55
+ return builder.build();
56
+ }
57
+ }
@@ -0,0 +1,183 @@
1
+ package org.embulk.input.cloudwatch_logs.aws;
2
+
3
+ import com.amazonaws.auth.AWSCredentials;
4
+ import com.amazonaws.auth.AWSCredentialsProvider;
5
+ import com.amazonaws.auth.AWSSessionCredentials;
6
+ import com.amazonaws.auth.AWSSessionCredentialsProvider;
7
+ import com.amazonaws.auth.AnonymousAWSCredentials;
8
+ import com.amazonaws.auth.BasicAWSCredentials;
9
+ import com.amazonaws.auth.BasicSessionCredentials;
10
+ import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
11
+ import com.amazonaws.auth.EnvironmentVariableCredentialsProvider;
12
+ import com.amazonaws.auth.InstanceProfileCredentialsProvider;
13
+ import com.amazonaws.auth.SystemPropertiesCredentialsProvider;
14
+ import com.amazonaws.auth.profile.ProfileCredentialsProvider;
15
+ import com.amazonaws.auth.profile.ProfilesConfigFile;
16
+ import org.embulk.config.ConfigException;
17
+ import org.embulk.spi.Exec;
18
+ import org.slf4j.Logger;
19
+
20
+ import java.util.Optional;
21
+
22
+ public abstract class AwsCredentials
23
+ {
24
+ private AwsCredentials()
25
+ {
26
+ }
27
+
28
+ public static AWSCredentialsProvider getAWSCredentialsProvider(AwsCredentialsConfig task)
29
+ {
30
+ String authenticationMethodOption = "authentication_method";
31
+ String awsSessionTokenOption = "aws_session_token";
32
+ String awsAccessKeyIdOption = "aws_access_key_id";
33
+ String awsSecretAccessKeyOption = "aws_secret_access_key";
34
+ String awsProfileFileOption = "aws_profile_file";
35
+ String awsProfileNameOption = "aws_profile_name";
36
+
37
+ switch (task.getAuthenticationMethod()) {
38
+ case "basic": {
39
+ String accessKeyId = required(task.getAwsAccessKeyId(), "'" + awsAccessKeyIdOption + "', '" + awsSecretAccessKeyOption + "'");
40
+ String secretAccessKey = required(task.getAwsSecretAccessKey(), "'" + awsSecretAccessKeyOption + "'");
41
+ invalid(task.getAwsProfileFile(), awsProfileFileOption);
42
+ invalid(task.getAwsProfileName(), awsProfileNameOption);
43
+
44
+ final BasicAWSCredentials creds = new BasicAWSCredentials(accessKeyId, secretAccessKey);
45
+ return new AWSCredentialsProvider() {
46
+ public AWSCredentials getCredentials()
47
+ {
48
+ return creds;
49
+ }
50
+
51
+ public void refresh()
52
+ {
53
+ }
54
+ };
55
+ }
56
+
57
+ case "env":
58
+ invalid(task.getAwsAccessKeyId(), awsAccessKeyIdOption);
59
+ invalid(task.getAwsSecretAccessKey(), awsSecretAccessKeyOption);
60
+ invalid(task.getAwsProfileFile(), awsProfileFileOption);
61
+ invalid(task.getAwsProfileName(), awsProfileNameOption);
62
+
63
+ return overwriteAwsCredentials(task, new EnvironmentVariableCredentialsProvider().getCredentials());
64
+
65
+ case "instance":
66
+ invalid(task.getAwsAccessKeyId(), awsAccessKeyIdOption);
67
+ invalid(task.getAwsSecretAccessKey(), awsSecretAccessKeyOption);
68
+ invalid(task.getAwsProfileFile(), awsProfileFileOption);
69
+ invalid(task.getAwsProfileName(), awsProfileNameOption);
70
+
71
+ return new InstanceProfileCredentialsProvider();
72
+
73
+ case "profile":
74
+ {
75
+ invalid(task.getAwsAccessKeyId(), awsAccessKeyIdOption);
76
+ invalid(task.getAwsSecretAccessKey(), awsSecretAccessKeyOption);
77
+
78
+ String profileName = task.getAwsProfileName().orElse("default");
79
+ ProfileCredentialsProvider provider;
80
+ if (task.getAwsProfileFile().isPresent()) {
81
+ ProfilesConfigFile file = new ProfilesConfigFile(task.getAwsProfileFile().get().getFile());
82
+ provider = new ProfileCredentialsProvider(file, profileName);
83
+ }
84
+ else {
85
+ provider = new ProfileCredentialsProvider(profileName);
86
+ }
87
+
88
+ return overwriteAwsCredentials(task, provider.getCredentials());
89
+ }
90
+
91
+ case "properties":
92
+ invalid(task.getAwsAccessKeyId(), awsAccessKeyIdOption);
93
+ invalid(task.getAwsSecretAccessKey(), awsSecretAccessKeyOption);
94
+ invalid(task.getAwsProfileFile(), awsProfileFileOption);
95
+ invalid(task.getAwsProfileName(), awsProfileNameOption);
96
+
97
+ return overwriteAwsCredentials(task, new SystemPropertiesCredentialsProvider().getCredentials());
98
+
99
+ case "anonymous":
100
+ invalid(task.getAwsAccessKeyId(), awsAccessKeyIdOption);
101
+ invalid(task.getAwsSecretAccessKey(), awsSecretAccessKeyOption);
102
+ invalid(task.getAwsProfileFile(), awsProfileFileOption);
103
+ invalid(task.getAwsProfileName(), awsProfileNameOption);
104
+ return new AWSCredentialsProvider() {
105
+ public AWSCredentials getCredentials()
106
+ {
107
+ return new AnonymousAWSCredentials();
108
+ }
109
+
110
+ public void refresh()
111
+ {
112
+ }
113
+ };
114
+
115
+ case "session":
116
+ {
117
+ String accessKeyId = required(task.getAwsAccessKeyId(),
118
+ "'" + awsAccessKeyIdOption + "', '" + awsSecretAccessKeyOption + "', '" + awsSessionTokenOption + "'");
119
+ String secretAccessKey = required(task.getAwsSecretAccessKey(), "'" + awsSecretAccessKeyOption + "', '" + awsSessionTokenOption + "'");
120
+ String sessionToken = required(task.getAwsSessionToken(),
121
+ "'" + awsSessionTokenOption + "'");
122
+ invalid(task.getAwsProfileFile(), awsProfileFileOption);
123
+ invalid(task.getAwsProfileName(), awsProfileNameOption);
124
+ final AWSSessionCredentials creds = new BasicSessionCredentials(accessKeyId, secretAccessKey, sessionToken);
125
+ return new AWSSessionCredentialsProvider() {
126
+ public AWSSessionCredentials getCredentials()
127
+ {
128
+ return creds;
129
+ }
130
+
131
+ public void refresh()
132
+ {
133
+ }
134
+ };
135
+ }
136
+
137
+ case "default":
138
+ {
139
+ invalid(task.getAwsAccessKeyId(), awsAccessKeyIdOption);
140
+ invalid(task.getAwsSecretAccessKey(), awsSecretAccessKeyOption);
141
+ invalid(task.getAwsProfileFile(), awsProfileFileOption);
142
+ invalid(task.getAwsProfileName(), awsProfileNameOption);
143
+
144
+ return new DefaultAWSCredentialsProviderChain();
145
+ }
146
+
147
+ default:
148
+ throw new ConfigException(String.format("Unknown authentication_method '%s'. Supported methods are basic, env, instance, profile, properties, anonymous, and default.",
149
+ task.getAuthenticationMethod()));
150
+ }
151
+ }
152
+
153
+ private static AWSCredentialsProvider overwriteAwsCredentials(AwsCredentialsConfig task, final AWSCredentials creds)
154
+ {
155
+ return new AWSCredentialsProvider() {
156
+ public AWSCredentials getCredentials()
157
+ {
158
+ return creds;
159
+ }
160
+
161
+ public void refresh()
162
+ {
163
+ }
164
+ };
165
+ }
166
+
167
+ private static <T> T required(Optional<T> value, String message)
168
+ {
169
+ if (value.isPresent()) {
170
+ return value.get();
171
+ }
172
+ else {
173
+ throw new ConfigException("Required option is not set: " + message);
174
+ }
175
+ }
176
+
177
+ private static <T> void invalid(Optional<T> value, String message)
178
+ {
179
+ if (value.isPresent()) {
180
+ throw new ConfigException("Invalid option is set: " + message);
181
+ }
182
+ }
183
+ }
@@ -0,0 +1,32 @@
1
+ package org.embulk.input.cloudwatch_logs.aws;
2
+
3
+ import org.embulk.spi.unit.LocalFile;
4
+
5
+ import java.util.Optional;
6
+
7
+ public interface AwsCredentialsConfig
8
+ {
9
+ String getAuthenticationMethod();
10
+
11
+ void setAuthenticationMethod(String method);
12
+
13
+ Optional<String> getAwsAccessKeyId();
14
+
15
+ void setAwsAccessKeyId(Optional<String> value);
16
+
17
+ Optional<String> getAwsSecretAccessKey();
18
+
19
+ void setAwsSecretAccessKey(Optional<String> value);
20
+
21
+ Optional<String> getAwsSessionToken();
22
+
23
+ void setAwsSessionToken(Optional<String> value);
24
+
25
+ Optional<LocalFile> getAwsProfileFile();
26
+
27
+ void setAwsProfileFile(Optional<String> value);
28
+
29
+ Optional<String> getAwsProfileName();
30
+
31
+ void setAwsProfileName(Optional<String> value);
32
+ }
@@ -0,0 +1,41 @@
1
+ package org.embulk.input.cloudwatch_logs.aws;
2
+
3
+ import org.embulk.config.Config;
4
+ import org.embulk.config.ConfigDefault;
5
+ import org.embulk.spi.unit.LocalFile;
6
+
7
+ import java.util.Optional;
8
+
9
+ public interface AwsCredentialsTask
10
+ extends AwsCredentialsConfig
11
+ {
12
+ @Override
13
+ @Config("authentication_method")
14
+ @ConfigDefault("\"basic\"")
15
+ String getAuthenticationMethod();
16
+
17
+ @Override
18
+ @Config("aws_access_key_id")
19
+ @ConfigDefault("null")
20
+ Optional<String> getAwsAccessKeyId();
21
+
22
+ @Override
23
+ @Config("aws_secret_access_key")
24
+ @ConfigDefault("null")
25
+ Optional<String> getAwsSecretAccessKey();
26
+
27
+ @Override
28
+ @Config("aws_session_token")
29
+ @ConfigDefault("null")
30
+ Optional<String> getAwsSessionToken();
31
+
32
+ @Override
33
+ @Config("aws_profile_file")
34
+ @ConfigDefault("null")
35
+ Optional<LocalFile> getAwsProfileFile();
36
+
37
+ @Override
38
+ @Config("aws_profile_name")
39
+ @ConfigDefault("null")
40
+ Optional<String> getAwsProfileName();
41
+ }
@@ -0,0 +1,188 @@
1
+ package org.embulk.input.cloudwatch_logs;
2
+
3
+ import com.amazonaws.services.logs.AWSLogsClient;
4
+ import com.amazonaws.services.logs.AWSLogsClientBuilder;
5
+ import com.amazonaws.services.logs.AWSLogs;
6
+ import com.amazonaws.services.logs.model.DescribeLogStreamsRequest;
7
+ import com.amazonaws.services.logs.model.DescribeLogStreamsResult;
8
+ import com.amazonaws.services.logs.model.LogStream;
9
+ import com.amazonaws.services.logs.model.GetLogEventsRequest;
10
+ import com.amazonaws.services.logs.model.GetLogEventsResult;
11
+ import com.amazonaws.services.logs.model.OutputLogEvent;
12
+
13
+ import org.embulk.EmbulkTestRuntime;
14
+ import org.embulk.config.ConfigDiff;
15
+ import org.embulk.config.ConfigSource;
16
+ import org.embulk.config.TaskReport;
17
+ import org.embulk.config.TaskSource;
18
+ import org.embulk.spi.InputPlugin;
19
+ import org.embulk.spi.PageBuilder;
20
+ import org.embulk.spi.PageOutput;
21
+ import org.embulk.spi.Schema;
22
+ import org.embulk.spi.TestPageBuilderReader.MockPageOutput;
23
+ import org.embulk.spi.util.Pages;
24
+ import org.embulk.test.EmbulkTests;
25
+ import org.embulk.test.TestingEmbulk;
26
+
27
+ import org.junit.Before;
28
+ import org.junit.BeforeClass;
29
+ import org.junit.Rule;
30
+ import org.junit.Test;
31
+ import org.mockito.Mockito;
32
+ import org.mockito.Spy;
33
+
34
+ import java.io.IOException;
35
+ import java.util.ArrayList;
36
+ import java.util.List;
37
+ import org.embulk.input.cloudwatch_logs.CloudwatchLogsInputPlugin;
38
+
39
+ import static org.embulk.input.cloudwatch_logs.CloudwatchLogsInputPlugin.CloudWatchLogsPluginTask;
40
+ import static org.junit.Assert.assertEquals;
41
+ import static org.junit.Assert.assertFalse;
42
+ import static org.junit.Assert.assertNull;
43
+ import static org.junit.Assume.assumeNotNull;
44
+ import static org.junit.Assert.assertEquals;
45
+ import static org.junit.Assert.assertTrue;
46
+ import static org.mockito.Mockito.doReturn;
47
+ import static org.mockito.Mockito.times;
48
+ import static org.mockito.Mockito.verify;
49
+ import static org.mockito.Mockito.when;
50
+
51
+ public class TestAwsCredentials
52
+ {
53
+ @Rule
54
+ public EmbulkTestRuntime runtime = new EmbulkTestRuntime();
55
+
56
+ @Rule
57
+ public TestingEmbulk embulk = TestingEmbulk.builder()
58
+ .registerPlugin(InputPlugin.class, "cloudwatch_logs", CloudwatchLogsInputPlugin.class)
59
+ .build();
60
+
61
+ private CloudwatchLogsInputPlugin plugin;
62
+
63
+ private ConfigSource config;
64
+ private MockPageOutput output = new MockPageOutput();
65
+ private PageBuilder pageBuilder;
66
+
67
+ private static String EMBULK_LOGS_TEST_GROUP_NAME;
68
+ private static String EMBULK_LOGS_TEST_STREAM_NAME;
69
+ private static String EMBULK_LOGS_TEST_REGION;
70
+ private static String EMBULK_LOGS_TEST_ACCESS_KEY_ID;
71
+ private static String EMBULK_LOGS_TEST_SECRET_ACCESS_KEY;
72
+
73
+ /*
74
+ * This test case requires environment variables:
75
+ * EMBULK_LOGS_TEST_GROUP_NAME
76
+ * EMBULK_LOGS_TEST_STREAM_NAME
77
+ * EMBULK_LOGS_TEST_REGION
78
+ * EMBULK_LOGS_TEST_ACCESS_KEY_ID
79
+ * EMBULK_LOGS_TEST_SECRET_ACCESS_KEY
80
+ * If the variables not set, the test case is skipped.
81
+ */
82
+ @BeforeClass
83
+ public static void initializeConstantVariables()
84
+ {
85
+ EMBULK_LOGS_TEST_GROUP_NAME = System.getenv("EMBULK_LOGS_TEST_GROUP_NAME");
86
+ EMBULK_LOGS_TEST_STREAM_NAME = System.getenv("EMBULK_LOGS_TEST_STREAM_NAME");
87
+ EMBULK_LOGS_TEST_REGION = System.getenv("EMBULK_LOGS_TEST_REGION");
88
+ EMBULK_LOGS_TEST_ACCESS_KEY_ID = System.getenv("EMBULK_LOGS_TEST_ACCESS_KEY_ID");
89
+ EMBULK_LOGS_TEST_SECRET_ACCESS_KEY = System.getenv("EMBULK_LOGS_TEST_SECRET_ACCESS_KEY");
90
+ assumeNotNull(EMBULK_LOGS_TEST_GROUP_NAME, EMBULK_LOGS_TEST_STREAM_NAME, EMBULK_LOGS_TEST_REGION, EMBULK_LOGS_TEST_ACCESS_KEY_ID, EMBULK_LOGS_TEST_SECRET_ACCESS_KEY);
91
+ }
92
+
93
+ @Before
94
+ public void setUp() throws IOException
95
+ {
96
+ if (plugin == null) {
97
+ plugin = Mockito.spy(new CloudwatchLogsInputPlugin());
98
+ config = runtime.getExec().newConfigSource()
99
+ .set("type", "cloudwatch_logs")
100
+ .set("log_group_name", EMBULK_LOGS_TEST_GROUP_NAME)
101
+ .set("log_stream_name", EMBULK_LOGS_TEST_STREAM_NAME)
102
+ .set("use_log_stream_name_prefix", "true")
103
+ .set("region", EMBULK_LOGS_TEST_REGION);
104
+ pageBuilder = Mockito.mock(PageBuilder.class);
105
+ doReturn(pageBuilder).when(plugin).getPageBuilder(Mockito.any(), Mockito.any());
106
+ }
107
+ }
108
+
109
+ private void doTest(ConfigSource config) throws IOException
110
+ {
111
+ plugin.transaction(config, new Control());
112
+ verify(pageBuilder, times(1)).finish();
113
+ }
114
+
115
+ @Test
116
+ public void useBasic() throws IOException
117
+ {
118
+ ConfigSource config = this.config.deepCopy()
119
+ .set("authentication_method", "basic")
120
+ .set("aws_access_key_id", EMBULK_LOGS_TEST_ACCESS_KEY_ID)
121
+ .set("aws_secret_access_key", EMBULK_LOGS_TEST_SECRET_ACCESS_KEY);
122
+ doTest(config);
123
+ }
124
+
125
+ @Test
126
+ public void useEnv()
127
+ {
128
+ // TODO
129
+ }
130
+
131
+ @Test
132
+ public void useInstance()
133
+ {
134
+ // TODO
135
+ }
136
+
137
+ @Test
138
+ public void useProfile()
139
+ {
140
+ // TODO
141
+ }
142
+
143
+ @Test
144
+ public void useProperties() throws IOException
145
+ {
146
+ String prevAccessKeyId = System.getProperty("aws.accessKeyId");
147
+ String prevSecretKey = System.getProperty("aws.secretKey");
148
+ try {
149
+ ConfigSource config = this.config.deepCopy().set("authentication_method", "properties");
150
+ System.setProperty("aws.accessKeyId", EMBULK_LOGS_TEST_ACCESS_KEY_ID);
151
+ System.setProperty("aws.secretKey", EMBULK_LOGS_TEST_SECRET_ACCESS_KEY);
152
+ doTest(config);
153
+ }
154
+ finally {
155
+ if (prevAccessKeyId != null) {
156
+ System.setProperty("aws.accessKeyId", prevAccessKeyId);
157
+ }
158
+ if (prevSecretKey != null) {
159
+ System.setProperty("aws.secretKey", prevSecretKey);
160
+ }
161
+ }
162
+ }
163
+
164
+ @Test
165
+ public void useAnonymous()
166
+ {
167
+ // TODO
168
+ }
169
+
170
+ @Test
171
+ public void useSession()
172
+ {
173
+ //TODO
174
+ }
175
+
176
+ private class Control implements InputPlugin.Control
177
+ {
178
+ @Override
179
+ public List<TaskReport> run(final TaskSource taskSource, final Schema schema, final int taskCount)
180
+ {
181
+ List<TaskReport> reports = new ArrayList<>();
182
+ for (int i = 0; i < taskCount; i++) {
183
+ reports.add(plugin.run(taskSource, schema, i, output));
184
+ }
185
+ return reports;
186
+ }
187
+ }
188
+ }