001/*
002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Remi Cattiau
018 *     Florent Guillaume
019 */
020package org.nuxeo.runtime.aws;
021
022import static org.apache.commons.lang3.StringUtils.isNotBlank;
023import static org.nuxeo.runtime.aws.AWSConfigurationDescriptor.DEFAULT_CONFIG_ID;
024
025import org.nuxeo.runtime.model.DefaultComponent;
026
027import com.amazonaws.auth.AWSCredentials;
028import com.amazonaws.auth.BasicAWSCredentials;
029import com.amazonaws.auth.BasicSessionCredentials;
030
031/**
032 * Implementation of the service providing AWS configuration.
033 * <p>
034 * This service does a simple lookup in provided Nuxeo configuration. Instead of this service, you should probably use
035 * {@link NuxeoAWSCredentialsProvider} and {@link NuxeoAWSRegionProvider} because they fall back to the default AWS SDK
036 * lookup behavior if no Nuxeo configuration is available.
037 *
038 * @since 10.3
039 * @see NuxeoAWSCredentialsProvider
040 * @see NuxeoAWSRegionProvider
041 */
042public class AWSConfigurationServiceImpl extends DefaultComponent implements AWSConfigurationService {
043
044    public static final String XP_CONFIGURATION = "configuration";
045
046    @Override
047    public AWSCredentials getAWSCredentials(String id) {
048        if (id == null) {
049            id = DEFAULT_CONFIG_ID;
050        }
051        AWSConfigurationDescriptor descriptor = getDescriptor(XP_CONFIGURATION, id);
052        if (descriptor != null) {
053            String accessKeyId = descriptor.getAccessKeyId();
054            String secretKey = descriptor.getSecretKey();
055            String sessionToken = descriptor.getSessionToken();
056            if (isNotBlank(accessKeyId) && isNotBlank(secretKey)) {
057                if (isNotBlank(sessionToken)) {
058                    return new BasicSessionCredentials(accessKeyId, secretKey, sessionToken);
059                } else {
060                    return new BasicAWSCredentials(accessKeyId, secretKey);
061                }
062            }
063        }
064        return null;
065    }
066
067    @Override
068    public String getAWSRegion(String id) {
069        if (id == null) {
070            id = DEFAULT_CONFIG_ID;
071        }
072        AWSConfigurationDescriptor descriptor = getDescriptor(XP_CONFIGURATION, id);
073        if (descriptor != null) {
074            String region = descriptor.getRegion();
075            if (isNotBlank(region)) {
076                return region;
077            }
078        }
079        return null;
080    }
081
082}