001/** 002 * Copyright 2004-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php 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 */ 016package org.kuali.common.aws.model; 017 018import org.kuali.common.core.build.ValidatingBuilder; 019import org.kuali.common.core.json.jackson.DecryptingDeserializer; 020import org.kuali.common.core.json.jackson.EncryptingSerializer; 021import org.kuali.common.core.validate.annotation.IdiotProofImmutable; 022 023import com.amazonaws.auth.AWSCredentials; 024import com.amazonaws.auth.AWSSessionCredentials; 025import com.fasterxml.jackson.annotation.JsonIgnore; 026import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 027import com.fasterxml.jackson.databind.annotation.JsonSerialize; 028 029@IdiotProofImmutable 030@JsonDeserialize(builder = ImmutableAWSSessionCredentials.Builder.class) 031public final class ImmutableAWSSessionCredentials implements AWSSessionCredentials { 032 033 @JsonSerialize(using = EncryptingSerializer.class) 034 private final String sessionToken; 035 private final ImmutableAWSCredentials credentials; 036 037 private ImmutableAWSSessionCredentials(Builder builder) { 038 this.sessionToken = builder.sessionToken; 039 this.credentials = ImmutableAWSCredentials.copyOf(builder.credentials); 040 } 041 042 public static ImmutableAWSSessionCredentials copyOf(AWSSessionCredentials credentials) { 043 if (credentials instanceof ImmutableAWSSessionCredentials) { 044 return (ImmutableAWSSessionCredentials) credentials; 045 } else { 046 return build(credentials.getAWSAccessKeyId(), credentials.getAWSSecretKey(), credentials.getSessionToken()); 047 } 048 } 049 050 public static ImmutableAWSSessionCredentials build(AWSCredentials credentials, String sessionToken) { 051 Builder builder = builder(); 052 builder.withCredentials(ImmutableAWSCredentials.copyOf(credentials)); 053 builder.withSessionToken(sessionToken); 054 return builder.build(); 055 } 056 057 public static ImmutableAWSSessionCredentials build(String accessKey, String secretKey, String sessionToken) { 058 return build(ImmutableAWSCredentials.build(accessKey, secretKey), sessionToken); 059 } 060 061 public static Builder builder() { 062 return new Builder(); 063 } 064 065 public static class Builder extends ValidatingBuilder<ImmutableAWSSessionCredentials> { 066 067 @JsonDeserialize(as = ImmutableAWSCredentials.class) 068 private AWSCredentials credentials; 069 070 @JsonDeserialize(using = DecryptingDeserializer.class) 071 private String sessionToken; 072 073 public Builder withCredentials(AWSCredentials credentials) { 074 this.credentials = credentials; 075 return this; 076 } 077 078 public Builder withSessionToken(String sessionToken) { 079 this.sessionToken = sessionToken; 080 return this; 081 } 082 083 @Override 084 public ImmutableAWSSessionCredentials build() { 085 return validate(new ImmutableAWSSessionCredentials(this)); 086 } 087 } 088 089 @Override 090 public String getSessionToken() { 091 return sessionToken; 092 } 093 094 public AWSCredentials getCredentials() { 095 return credentials; 096 } 097 098 @Override 099 @JsonIgnore 100 public String getAWSAccessKeyId() { 101 return credentials.getAccessKey(); 102 } 103 104 @Override 105 @JsonIgnore 106 public String getAWSSecretKey() { 107 return credentials.getSecretKey(); 108 } 109 110}