001package com.pingidentity.ds.log;
002
003import com.amazonaws.auth.*;
004import com.amazonaws.auth.profile.ProfileCredentialsProvider;
005import com.amazonaws.regions.Regions;
006import com.amazonaws.services.s3.AmazonS3;
007import com.amazonaws.services.s3.AmazonS3ClientBuilder;
008import com.amazonaws.services.s3.model.PutObjectResult;
009import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
010import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
011import com.amazonaws.services.securitytoken.model.AssumeRoleRequest;
012import com.amazonaws.services.securitytoken.model.AssumeRoleResult;
013import com.amazonaws.services.securitytoken.model.Credentials;
014import com.unboundid.directory.sdk.common.api.LogFileRotationListener;
015import com.unboundid.directory.sdk.common.config.LogFileRotationListenerConfig;
016import com.unboundid.directory.sdk.common.types.RegisteredMonitorProvider;
017import com.unboundid.directory.sdk.common.types.ServerContext;
018import com.unboundid.ldap.sdk.LDAPException;
019import com.unboundid.ldap.sdk.ResultCode;
020import com.unboundid.util.args.ArgumentException;
021import com.unboundid.util.args.ArgumentParser;
022import com.pingidentity.arno.args.*;
023import com.unboundid.util.args.FileArgument;
024import com.unboundid.util.args.StringArgument;
025
026import java.io.File;
027import java.util.ArrayList;
028import java.util.List;
029import java.util.concurrent.atomic.AtomicInteger;
030
031import static sun.misc.MessageUtils.out;
032
033/**
034 * This class provides a simple LogFileRotationListener that uploads the log file to S3 upon rotation
035 *
036 * Several methods are supported for authentication:
037 *   - explicitly set credentials on the extension with access-key and secret-key arguments
038 *   - use a session token in combination with access-key and secret-key
039 *   - use a property file
040 *   - use environment variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY)
041 *   - use Java system properties aws.accessKeyId and aws.secretKey
042 *   - use credential profiles file at the default location (~/.aws/credentials)
043 *   - use credentials delivered through the Amazon EC2 container service if AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" environment variable is set and security manager has permission to access the variable
044 *   - use instance profile credentials delivered through the Amazon EC2 metadata service
045 *
046 * Required argument(s):
047 *   - bucket-name : the name of the bucket to uploads the files to upon rotation
048 *
049 */
050public class S3LogRotationListener extends LogFileRotationListener {
051    public static final String ARG_NAME_BUCKET = "bucket-name";
052    public static final String ARG_NAME_PATH = "bucket-path";
053    public static final String ARG_NAME_ACCESS_KEY = "access-key";
054    public static final String ARG_NAME_SECRET_KEY = "secret-key";
055    public static final String ARG_NAME_REGION = "region";
056    public static final String ARG_NAME_SESSION = "session-token";
057    public static final String ARG_NAME_PROPERTIES = "properties-file";
058    public static final String ARG_NAME_ROLE_ARN = "role-arn";
059    public static final String ARG_NAME_ROLE_NAME = "role-name";
060
061    AmazonS3 s3Client;
062    private String bucket;
063    private String path;
064    AtomicInteger rotatedFileCount = new AtomicInteger(0);
065    LogFileRotationListenerConfig config;
066    private S3LogRotationListenerMonitorProvider monitorProvider;
067    private RegisteredMonitorProvider registeredMonitoredProvider;
068    private ServerContext serverContext;
069
070    /**
071     * Computes the extension name
072     * @return the extension name
073     */
074    @Override
075    public String getExtensionName() {
076        return "S3LogRotationListener";
077    }
078
079    /**
080     * Computes descriptive text about the extension
081     * @return a list of messages about the extension, each is displayed as a separate paragraph
082     */
083    @Override
084    public String[] getExtensionDescription() {
085        return new String[]{"This extension copies logs to an S3 bucket upon rotation by the server according to the configuration log rotation policy"};
086    }
087
088    /**
089     * Performs the necessary processing to declare the arguments that may be used to customize the behavior of the
090     * instance of the extension
091     * @param parser the argument parser used to register the arguments
092     * @throws ArgumentException if any issue is encountered whilst registering configuration arguments
093     */
094    @Override
095    public void defineConfigArguments(ArgumentParser parser) throws ArgumentException {
096        parser.addArgument(StringArg.getRequiredSingle(ARG_NAME_BUCKET, "The name of the S3 bucket to upload files upon log rotation"));
097        parser.addArgument(FileArg.getOptionalSingleDir(ARG_NAME_PATH, "The path in the bucket where the file must be placed"));
098
099        StringArgument accessArg = StringArg.getOptionalSingle(ARG_NAME_ACCESS_KEY, "The access key to authenticate to the bucket");
100        parser.addArgument(accessArg);
101
102        StringArgument secretArg = StringArg.getOptionalSingle(ARG_NAME_SECRET_KEY, "The secret key to authenticate to the bucket");
103        parser.addArgument(secretArg);
104        parser.addDependentArgumentSet(secretArg, accessArg);
105
106        StringArgument sessionArg = StringArg.getOptionalSingle(ARG_NAME_SESSION, "The session token to authenticate to the bucket");
107        parser.addArgument(sessionArg);
108        parser.addDependentArgumentSet(sessionArg, accessArg);
109
110        StringArgument roleArnArg = StringArg.getOptionalSingle(ARG_NAME_ROLE_ARN,"The ARN of the role to use to obtain short-lived credentials");
111        parser.addArgument(roleArnArg);
112        parser.addDependentArgumentSet(roleArnArg,accessArg);
113
114        parser.addExclusiveArgumentSet(sessionArg,roleArnArg);
115
116        StringArgument roleNameArg = StringArg.getOptionalSingle(ARG_NAME_ROLE_NAME,"The name of the role to use to obtain short-lived credentials");
117        parser.addArgument(roleNameArg);
118        parser.addDependentArgumentSet(roleNameArg,roleArnArg);
119
120        FileArgument propertiesArg = FileArg.getOptionalSingleFileThatExists(ARG_NAME_PROPERTIES, "The properties file to use to authenticate to the bucket");
121        parser.addArgument(propertiesArg);
122        parser.addExclusiveArgumentSet(propertiesArg, accessArg);
123
124        parser.addArgument(StringArg.getOptionalSingle(ARG_NAME_REGION, "The region the target S3 bucket is in"));
125    }
126
127    /**
128     * Performs the necessary processing to apply the configuration to the instance of the extension
129     * @param config the configuration of the instance of the extension
130     * @param parser argument parser to provide configuration input to the extension
131     * @param adminActionsRequired a list of messages detailing administrative action required
132     * @param messages a list of message about the applying configuration to the instance of the extension
133     * @return ResultCode.SUCCESS unless a problem was encountered that prevented successfully applying configuration
134     */
135    @Override
136    public ResultCode applyConfiguration(LogFileRotationListenerConfig config, ArgumentParser parser, List<String> adminActionsRequired, List<String> messages) {
137        this.config=config;
138        serverContext = config.getServerContext();
139        monitorProvider = new S3LogRotationListenerMonitorProvider(this);
140
141        if (registeredMonitoredProvider!= null)
142            serverContext.deregisterMonitorProvider(registeredMonitoredProvider);
143        registeredMonitoredProvider = serverContext.registerMonitorProvider(monitorProvider,config);
144
145        bucket = parser.getStringArgument(ARG_NAME_BUCKET).getValue();
146        File _path = parser.getFileArgument(ARG_NAME_PATH).getValue();
147        if (_path != null) {
148            String _pathString = _path.getPath();
149            if (!_pathString.isEmpty() && !_pathString.endsWith("/")) {
150                _pathString.concat("/");
151                path = _pathString;
152            } else {
153                path="";
154            }
155        }
156        String accessKey = parser.getStringArgument(ARG_NAME_ACCESS_KEY).getValue();
157        String secretKey = parser.getStringArgument(ARG_NAME_SECRET_KEY).getValue();
158        String sessionToken = parser.getStringArgument(ARG_NAME_REGION).getValue();
159        String roleARN = parser.getStringArgument(ARG_NAME_ROLE_ARN).getValue();
160        String roleName = parser.getStringArgument(ARG_NAME_ROLE_NAME).getValue();
161
162        File awsProperties = parser.getFileArgument(ARG_NAME_PROPERTIES).getValue();
163        String region = parser.getStringArgument(ARG_NAME_REGION).getValue();
164        Regions s3Region = region == null ? Regions.DEFAULT_REGION : Regions.fromName(region);
165
166        List<AWSCredentialsProvider> credentialsProviders = new ArrayList<>();
167
168        if (accessKey != null && secretKey != null) {
169            AWSCredentials s3Credentials = null;
170            if (sessionToken != null && ! sessionToken.isEmpty() ) {
171                s3Credentials = new BasicSessionCredentials(accessKey, secretKey, sessionToken);
172            } else {
173                s3Credentials = new BasicAWSCredentials(accessKey, secretKey);
174                if ( roleARN != null && ! roleARN.isEmpty() ){
175                    AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(s3Credentials)).withRegion(s3Region).build();
176                    AssumeRoleRequest roleRequest = new AssumeRoleRequest().withRoleArn(roleARN).withRoleSessionName(roleName);
177                    AssumeRoleResult roleResponse = stsClient.assumeRole(roleRequest);
178                    Credentials sessionCredentials = roleResponse.getCredentials();
179                    s3Credentials = new BasicSessionCredentials(sessionCredentials.getAccessKeyId(),sessionCredentials.getSecretAccessKey(),sessionCredentials.getSessionToken());
180                }
181            }
182            if (s3Credentials != null) {
183                credentialsProviders.add(new AWSStaticCredentialsProvider(s3Credentials));
184            }
185        }
186        if (awsProperties != null) {
187            credentialsProviders.add(new PropertiesFileCredentialsProvider(awsProperties.getPath()));
188        }
189
190        credentialsProviders.add(new EnvironmentVariableCredentialsProvider());
191        credentialsProviders.add(new SystemPropertiesCredentialsProvider());
192        credentialsProviders.add(new ProfileCredentialsProvider());
193        credentialsProviders.add(new EC2ContainerCredentialsProviderWrapper());
194
195        AWSCredentialsProviderChain cpChain = new AWSCredentialsProviderChain(credentialsProviders);
196        s3Client = AmazonS3ClientBuilder.standard().withRegion(s3Region).withCredentials(cpChain).build();
197        return ResultCode.SUCCESS;
198    }
199
200    /**
201     * Performs the necessary processing to initialize the instance of the extension
202     * @param serverContext the server context
203     * @param config the configuration for the instance of the extenion
204     * @param parser the argument parser
205     * @throws LDAPException in case any issue was encountered
206     */
207    @Override
208    public void initializeLogFileRotationListener(ServerContext serverContext, LogFileRotationListenerConfig config, ArgumentParser parser) throws LDAPException {
209        super.initializeLogFileRotationListener(serverContext, config, parser);
210        List<String> adminActionMessages = new ArrayList<>(3);
211        List<String> messages = new ArrayList<>(3);
212        ResultCode resultCode = applyConfiguration(config, parser, adminActionMessages, messages);
213        if (!ResultCode.SUCCESS.equals(resultCode)) {
214            adminActionMessages.forEach(s -> out(s));
215            messages.forEach(s -> out(s));
216        }
217    }
218
219    /**
220     * Gracefully shutdown
221     */
222    @Override
223    public void finalizeLogFileRotationListener() {
224        if (registeredMonitoredProvider!=null)
225            serverContext.deregisterMonitorProvider(registeredMonitoredProvider);
226    }
227
228    /**
229     * Upload the file to S3 upon log rotation
230     * @param file
231     * @throws LDAPException
232     */
233    @Override
234    public void doPostRotationProcessing(File file) throws LDAPException {
235        PutObjectResult putObjectResult = s3Client.putObject(bucket, path + file.getName(), file);
236        rotatedFileCount.incrementAndGet();
237    }
238}