Coverage Report - com.jcabi.simpledb.Credentials
 
Classes in this File Line Coverage Branch Coverage Complexity
Credentials
N/A
N/A
1
Credentials$Assumed
0%
0/14
0%
0/10
1
Credentials$Assumed$AjcClosure1
0%
0/1
N/A
1
Credentials$Direct
0%
0/12
0%
0/16
1
Credentials$Direct$AjcClosure1
0%
0/1
N/A
1
Credentials$Simple
88%
16/18
0%
0/22
1
Credentials$Simple$AjcClosure1
100%
1/1
N/A
1
 
 1  0
 /**
 2  
  * Copyright (c) 2012-2014, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.simpledb;
 31  
 
 32  
 import com.amazonaws.auth.BasicAWSCredentials;
 33  
 import com.amazonaws.regions.RegionUtils;
 34  
 import com.amazonaws.regions.Regions;
 35  
 import com.amazonaws.services.simpledb.AmazonSimpleDB;
 36  
 import com.amazonaws.services.simpledb.AmazonSimpleDBClient;
 37  
 import com.jcabi.aspects.Immutable;
 38  
 import com.jcabi.aspects.Loggable;
 39  
 import javax.validation.constraints.NotNull;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import org.apache.commons.lang3.Validate;
 42  
 
 43  
 /**
 44  
  * Amazon SimpleDB credentials.
 45  
  *
 46  
  * <p>It is recommended to use {@link Credentials.Simple} in most cases.
 47  
  *
 48  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 49  
  * @version $Id$
 50  
  * @since 0.1
 51  
  */
 52  
 @Immutable
 53  
 public interface Credentials {
 54  
 
 55  
     /**
 56  
      * Test credentials, for unit testing mostly.
 57  
      */
 58  
     Credentials TEST = new Credentials.Simple(
 59  
         "AAAAAAAAAAAAAAAAAAAA",
 60  
         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
 61  
     );
 62  
 
 63  
     /**
 64  
      * Build AWS client.
 65  
      *
 66  
      * <p>Don't forget to shut it down after use,
 67  
      * using {@link AmazonSimpleDB#shutdown()}.
 68  
      *
 69  
      * @return Amazon Dynamo DB client
 70  
      */
 71  
     @NotNull
 72  
     AmazonSimpleDB aws();
 73  
 
 74  
     /**
 75  
      * Simple implementation.
 76  
      */
 77  
     @Immutable
 78  
     @Loggable(Loggable.DEBUG)
 79  0
     @EqualsAndHashCode(of = { "key", "secret", "region" })
 80  
     final class Simple implements Credentials {
 81  
         /**
 82  
          * AWS key.
 83  
          */
 84  
         private final transient String key;
 85  
         /**
 86  
          * AWS secret.
 87  
          */
 88  
         private final transient String secret;
 89  
         /**
 90  
          * Region name.
 91  
          */
 92  
         private final transient String region;
 93  
         /**
 94  
          * Public ctor, with "us-east-1" region.
 95  
          * @param akey AWS key
 96  
          * @param scrt Secret
 97  
          */
 98  
         public Simple(@NotNull final String akey, @NotNull final String scrt) {
 99  2
             this(akey, scrt, Regions.US_EAST_1.getName());
 100  2
         }
 101  
         /**
 102  
          * Public ctor.
 103  
          * @param akey AWS key
 104  
          * @param scrt Secret
 105  
          * @param reg Region
 106  
          */
 107  
         public Simple(
 108  
             @NotNull(message = "AWS key can't be NULL") final String akey,
 109  
             @NotNull(message = "AWS secret can't be NULL") final String scrt,
 110  3
             @NotNull(message = "AWS region can't be NULL") final String reg) {
 111  3
             Validate.matchesPattern(
 112  
                 akey, "[A-Z0-9]{20}",
 113  
                 "Invalid AWS key '%s'", akey
 114  
             );
 115  3
             this.key = akey;
 116  3
             Validate.matchesPattern(
 117  
                 scrt, "[a-zA-Z0-9+/=]{40}",
 118  
                 "Invalid AWS secret key '%s'", scrt
 119  
             );
 120  3
             this.secret = scrt;
 121  3
             Validate.matchesPattern(
 122  
                 reg, "[-a-z0-9]+",
 123  
                 "Invalid AWS region name '%s'", reg
 124  
             );
 125  3
             this.region = reg;
 126  3
         }
 127  
         /**
 128  
          * {@inheritDoc}
 129  
          */
 130  
         @Override
 131  
         public String toString() {
 132  0
             return String.format("%s/%s", this.region, this.key);
 133  
         }
 134  
         /**
 135  
          * {@inheritDoc}
 136  
          */
 137  
         @Override
 138  
         @NotNull
 139  
         public AmazonSimpleDB aws() {
 140  4
             final AmazonSimpleDB aws = new AmazonSimpleDBClient(
 141  
                 new BasicAWSCredentials(this.key, this.secret)
 142  
             );
 143  2
             final com.amazonaws.regions.Region reg =
 144  
                 RegionUtils.getRegion(this.region);
 145  2
             Validate.notNull(reg, "Failed to find region '%s'", this.region);
 146  2
             aws.setRegion(reg);
 147  2
             return aws;
 148  
         }
 149  
     }
 150  
 
 151  
     /**
 152  
      * Assumed AWS IAM role.
 153  
      *
 154  
      * @see <a href="http://docs.aws.amazon.com/IAM/latest/UserGuide/role-usecase-ec2app.html">Granting Applications that Run on Amazon EC2 Instances Access to AWS Resources</a>
 155  
      */
 156  
     @Immutable
 157  
     @Loggable(Loggable.DEBUG)
 158  0
     @EqualsAndHashCode(of = "region")
 159  
     final class Assumed implements Credentials {
 160  
         /**
 161  
          * Region name.
 162  
          */
 163  
         private final transient String region;
 164  
         /**
 165  
          * Public ctor.
 166  
          */
 167  
         public Assumed() {
 168  0
             this(Regions.US_EAST_1.getName());
 169  0
         }
 170  
         /**
 171  
          * Public ctor.
 172  
          * @param reg Region
 173  
          */
 174  
         public Assumed(@NotNull(message = "SimpleDB region can't be NULL")
 175  0
             final String reg) {
 176  0
             Validate.matchesPattern(
 177  
                 reg, "[-0-9a-z]+",
 178  
                 "Invalid AWS region name: '%s'", reg
 179  
             );
 180  0
             this.region = reg;
 181  0
         }
 182  
         /**
 183  
          * {@inheritDoc}
 184  
          */
 185  
         @Override
 186  
         public String toString() {
 187  0
             return this.region;
 188  
         }
 189  
         /**
 190  
          * {@inheritDoc}
 191  
          */
 192  
         @Override
 193  
         @NotNull
 194  
         public AmazonSimpleDB aws() {
 195  0
             final AmazonSimpleDB aws = new AmazonSimpleDBClient();
 196  0
             final com.amazonaws.regions.Region reg =
 197  
                 RegionUtils.getRegion(this.region);
 198  0
             Validate.notNull(reg, "Failed to detect region '%s'", this.region);
 199  0
             aws.setRegion(reg);
 200  0
             return aws;
 201  
         }
 202  
     }
 203  
 
 204  
     /**
 205  
      * With explicitly specified endpoint.
 206  
      */
 207  
     @Immutable
 208  
     @Loggable(Loggable.DEBUG)
 209  0
     @EqualsAndHashCode(of = { "origin", "endpoint" })
 210  
     final class Direct implements Credentials {
 211  
         /**
 212  
          * Original credentials.
 213  
          */
 214  
         private final transient Credentials origin;
 215  
         /**
 216  
          * Endpoint.
 217  
          */
 218  
         private final transient String endpoint;
 219  
         /**
 220  
          * Public ctor.
 221  
          * @param creds Original credentials
 222  
          * @param pnt Endpoint
 223  
          */
 224  
         public Direct(
 225  
             @NotNull(message = "credentials is NULL") final Credentials creds,
 226  0
             @NotNull(message = "end point can't be NULL") final String pnt) {
 227  0
             this.origin = creds;
 228  0
             this.endpoint = pnt;
 229  0
         }
 230  
         /**
 231  
          * Public ctor.
 232  
          * @param creds Original credentials
 233  
          * @param port Port number for localhost
 234  
          */
 235  
         public Direct(@NotNull final Credentials creds, final int port) {
 236  0
             this(creds, String.format("http://localhost:%d", port));
 237  0
         }
 238  
         /**
 239  
          * {@inheritDoc}
 240  
          */
 241  
         @Override
 242  
         public String toString() {
 243  0
             return String.format("%s at %s", this.origin, this.endpoint);
 244  
         }
 245  
         /**
 246  
          * {@inheritDoc}
 247  
          */
 248  
         @Override
 249  
         @NotNull
 250  
         public AmazonSimpleDB aws() {
 251  0
             final AmazonSimpleDB aws = this.origin.aws();
 252  0
             aws.setEndpoint(this.endpoint);
 253  0
             return aws;
 254  
         }
 255  
     }
 256  
 
 257  
 }