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;
023
024import org.nuxeo.runtime.model.DefaultComponent;
025import org.nuxeo.runtime.model.Descriptor;
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 *
034 * @since 10.3
035 */
036public class AWSConfigurationServiceImpl extends DefaultComponent implements AWSConfigurationService {
037
038    public static final String XP_CONFIGURATION = "configuration";
039
040    protected AWSConfigurationDescriptor getDescriptor() {
041        return getDescriptor(XP_CONFIGURATION, Descriptor.UNIQUE_DESCRIPTOR_ID);
042    }
043
044    @Override
045    public AWSCredentials getAWSCredentials() {
046        AWSConfigurationDescriptor descriptor = getDescriptor();
047        if (descriptor != null) {
048            String accessKeyId = descriptor.getAccessKeyId();
049            String secretKey = descriptor.getSecretKey();
050            String sessionToken = descriptor.getSessionToken();
051            if (isNotBlank(accessKeyId) && isNotBlank(secretKey)) {
052                if (isNotBlank(sessionToken)) {
053                    return new BasicSessionCredentials(accessKeyId, secretKey, sessionToken);
054                } else {
055                    return new BasicAWSCredentials(accessKeyId, secretKey);
056                }
057            }
058        }
059        return null;
060    }
061
062    @Override
063    public String getAWSRegion() {
064        AWSConfigurationDescriptor descriptor = getDescriptor();
065        if (descriptor != null) {
066            String region = descriptor.getRegion();
067            if (isNotBlank(region)) {
068                return region;
069            }
070        }
071        return null;
072    }
073
074}