Coverage Report - com.jcabi.dynamo.Credentials
 
Classes in this File Line Coverage Branch Coverage Complexity
Credentials
N/A
N/A
1.308
Credentials$Assumed
0%
0/14
0%
0/14
1.308
Credentials$Assumed$AjcClosure1
0%
0/1
N/A
1.308
Credentials$Direct
0%
0/12
0%
0/20
1.308
Credentials$Direct$AjcClosure1
0%
0/1
N/A
1.308
Credentials$Simple
87%
14/16
6%
2/30
1.308
Credentials$Simple$AjcClosure1
100%
1/1
N/A
1.308
 
 1  0
 /**
 2  
  * Copyright (c) 2012-2016, 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.dynamo;
 31  
 
 32  
 import com.amazonaws.auth.BasicAWSCredentials;
 33  
 import com.amazonaws.regions.RegionUtils;
 34  
 import com.amazonaws.regions.Regions;
 35  
 import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
 36  
 import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
 37  
 import com.jcabi.aspects.Immutable;
 38  
 import com.jcabi.aspects.Loggable;
 39  
 import lombok.EqualsAndHashCode;
 40  
 
 41  
 /**
 42  
  * Amazon DynamoDB credentials.
 43  
  *
 44  
  * <p>It is recommended to use {@link Credentials.Simple} in most cases.
 45  
  *
 46  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 47  
  * @version $Id: c2b5c0813f6205003a5962347b17c17bae412f30 $
 48  
  * @since 0.1
 49  
  */
 50  
 @Immutable
 51  
 public interface Credentials {
 52  
 
 53  
     /**
 54  
      * Test credentials, for unit testing mostly.
 55  
      */
 56  
     Credentials TEST = new Credentials.Simple(
 57  
         "AAAAAAAAAAAAAAAAAAAA",
 58  
         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
 59  
     );
 60  
 
 61  
     /**
 62  
      * Build AWS client.
 63  
      *
 64  
      * <p>Don't forget to shut it down after use,
 65  
      * using {@link AmazonDynamoDB#shutdown()}.
 66  
      *
 67  
      * @return Amazon Dynamo DB client
 68  
      */
 69  
     AmazonDynamoDB aws();
 70  
 
 71  
     /**
 72  
      * Simple implementation.
 73  
      */
 74  
     @Immutable
 75  
     @Loggable(Loggable.DEBUG)
 76  1
     @EqualsAndHashCode(of = { "key", "secret", "region" })
 77  
     final class Simple implements Credentials {
 78  
         /**
 79  
          * AWS key.
 80  
          */
 81  
         private final transient String key;
 82  
         /**
 83  
          * AWS secret.
 84  
          */
 85  
         private final transient String secret;
 86  
         /**
 87  
          * Region name.
 88  
          */
 89  
         private final transient String region;
 90  
         /**
 91  
          * Public ctor, with "us-east-1" region.
 92  
          * @param akey AWS key
 93  
          * @param scrt Secret
 94  
          */
 95  
         public Simple(final String akey, final String scrt) {
 96  2
             this(akey, scrt, Regions.US_EAST_1.getName());
 97  2
         }
 98  
         /**
 99  
          * Public ctor.
 100  
          * @param akey AWS key
 101  
          * @param scrt Secret
 102  
          * @param reg Region
 103  
          */
 104  3
         public Simple(final String akey, final String scrt, final String reg) {
 105  3
             this.key = akey;
 106  3
             this.secret = scrt;
 107  3
             this.region = reg;
 108  3
         }
 109  
         @Override
 110  
         public String toString() {
 111  0
             return String.format("%s/%s", this.region, this.key);
 112  
         }
 113  
         @Override
 114  
         public AmazonDynamoDB aws() {
 115  4
             final com.amazonaws.regions.Region reg =
 116  
                 RegionUtils.getRegion(this.region);
 117  2
             if (reg == null) {
 118  0
                 throw new IllegalStateException(
 119  
                     String.format("Failed to find region '%s'", this.region)
 120  
                 );
 121  
             }
 122  2
             final AmazonDynamoDB aws = new AmazonDynamoDBClient(
 123  
                 new BasicAWSCredentials(this.key, this.secret)
 124  
             );
 125  2
             aws.setRegion(reg);
 126  2
             return aws;
 127  
         }
 128  
     }
 129  
 
 130  
     /**
 131  
      * Assumed AWS IAM role.
 132  
      *
 133  
      * @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>
 134  
      */
 135  
     @Immutable
 136  
     @Loggable(Loggable.DEBUG)
 137  0
     @EqualsAndHashCode(of = "region")
 138  
     final class Assumed implements Credentials {
 139  
         /**
 140  
          * Region name.
 141  
          */
 142  
         private final transient String region;
 143  
         /**
 144  
          * Public ctor.
 145  
          */
 146  
         public Assumed() {
 147  0
             this(Regions.US_EAST_1.getName());
 148  0
         }
 149  
         /**
 150  
          * Public ctor.
 151  
          * @param reg Region
 152  
          */
 153  0
         public Assumed(final String reg) {
 154  0
             this.region = reg;
 155  0
         }
 156  
         @Override
 157  
         public String toString() {
 158  0
             return this.region;
 159  
         }
 160  
         @Override
 161  
         public AmazonDynamoDB aws() {
 162  0
             final com.amazonaws.regions.Region reg =
 163  
                 RegionUtils.getRegion(this.region);
 164  0
             if (reg == null) {
 165  0
                 throw new IllegalStateException(
 166  
                     String.format("Failed to detect region '%s'", this.region)
 167  
                 );
 168  
             }
 169  0
             final AmazonDynamoDB aws = new AmazonDynamoDBClient();
 170  0
             aws.setRegion(reg);
 171  0
             return aws;
 172  
         }
 173  
     }
 174  
 
 175  
     /**
 176  
      * With explicitly specified endpoint.
 177  
      */
 178  
     @Immutable
 179  
     @Loggable(Loggable.DEBUG)
 180  0
     @EqualsAndHashCode(of = { "origin", "endpoint" })
 181  
     final class Direct implements Credentials {
 182  
         /**
 183  
          * Original credentials.
 184  
          */
 185  
         private final transient Credentials origin;
 186  
         /**
 187  
          * Endpoint.
 188  
          */
 189  
         private final transient String endpoint;
 190  
         /**
 191  
          * Public ctor.
 192  
          * @param creds Original credentials
 193  
          * @param pnt Endpoint
 194  
          */
 195  0
         public Direct(final Credentials creds, final String pnt) {
 196  0
             this.origin = creds;
 197  0
             this.endpoint = pnt;
 198  0
         }
 199  
         /**
 200  
          * Public ctor.
 201  
          * @param creds Original credentials
 202  
          * @param port Port number for localhost
 203  
          */
 204  
         public Direct(final Credentials creds, final int port) {
 205  0
             this(creds, String.format("http://localhost:%d", port));
 206  0
         }
 207  
         @Override
 208  
         public String toString() {
 209  0
             return String.format("%s at %s", this.origin, this.endpoint);
 210  
         }
 211  
         @Override
 212  
         public AmazonDynamoDB aws() {
 213  0
             final AmazonDynamoDB aws = this.origin.aws();
 214  0
             aws.setEndpoint(this.endpoint);
 215  0
             return aws;
 216  
         }
 217  
     }
 218  
 }