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