com.github.nethad.clustermeister.provisioning.ec2.AmazonGeneratedKeyPairCredentials.java Source code

Java tutorial

Introduction

Here is the source code for com.github.nethad.clustermeister.provisioning.ec2.AmazonGeneratedKeyPairCredentials.java

Source

/*
 * Copyright 2012 The Clustermeister Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.nethad.clustermeister.provisioning.ec2;

import com.github.nethad.clustermeister.api.Credentials;
import com.github.nethad.clustermeister.api.impl.KeyPairCredentials;
import com.google.common.base.Objects;
import com.google.common.base.Supplier;
import com.google.common.collect.ComparisonChain;
import java.io.File;

/**
 * A class representing a user name and key pair credential auto-generated by 
 * AWS EC2.
 *
 * @author daniel
 */
class AmazonGeneratedKeyPairCredentials extends KeyPairCredentials {
    /**
     * The private key.
     */
    final String privateKeyData;

    /**
     * The SHA-1 digest of the private key.
     */
    final Supplier<String> privateKeyDigest;

    /**
     * Creates a credential with user name and key pair.
     * 
     * @param user  the user name.
     * @param privateKeyData    the private key.
     */
    AmazonGeneratedKeyPairCredentials(String user, String privateKeyData) {
        this(AmazonGeneratedKeyPairCredentials.class.getSimpleName(), user, privateKeyData);
    }

    /**
     * Creates a credential with user name and key pair.
     * 
     * @param name A name for these credentials.
     * @param user  the user name.
     * @param privateKeyData    the private key.
     */
    AmazonGeneratedKeyPairCredentials(String name, String user, String privateKeyData) {
        super(name, user, new DummyFile("dummypath"));
        this.privateKeyData = privateKeyData;
        this.privateKeyDigest = getSha256DigestSupplier(privateKeyData, charset);
    }

    @Override
    public String getPrivateKey() {
        return privateKeyData;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != (getClass())) {
            return false;
        }
        AmazonGeneratedKeyPairCredentials other = (AmazonGeneratedKeyPairCredentials) obj;
        return Objects.equal(name, other.name) && Objects.equal(user, other.user)
                && Objects.equal(privateKeyDigest.get(), other.privateKeyDigest.get());
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(name, user, privateKeyDigest.get());
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(name).add("user", user)
                .add("privateKey", String.format("(sha-256:%s)", privateKeyDigest.get())).toString();
    }

    @Override
    public int compareTo(Credentials o) {
        ComparisonChain chain = ComparisonChain.start().compare(name, o.getName()).compare(user, o.getUser());
        if (o instanceof AmazonGeneratedKeyPairCredentials) {
            chain.compare(privateKeyDigest.get(),
                    o.as(AmazonGeneratedKeyPairCredentials.class).privateKeyDigest.get());
        }
        return chain.result();
    }

    private static class DummyFile extends File {
        public DummyFile(String pathname) {
            super(pathname);
        }

        @Override
        public boolean canRead() {
            return true;
        }

        @Override
        public boolean exists() {
            return true;
        }
    }
}