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