mirror of https://github.com/apache/jclouds.git
allow images to get credentials by credentialstore
This commit is contained in:
parent
c418edb140
commit
c6d1f2d56e
|
@ -20,6 +20,8 @@ package org.jclouds.compute.strategy.impl;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkState;
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
@ -38,10 +40,13 @@ import org.jclouds.javax.annotation.Nullable;
|
||||||
public class ReturnCredentialsBoundToImage implements PopulateDefaultLoginCredentialsForImageStrategy {
|
public class ReturnCredentialsBoundToImage implements PopulateDefaultLoginCredentialsForImageStrategy {
|
||||||
|
|
||||||
protected final LoginCredentials creds;
|
protected final LoginCredentials creds;
|
||||||
|
protected Map<String, Credentials> credentialStore;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ReturnCredentialsBoundToImage(@Nullable @Named("image") LoginCredentials creds) {
|
public ReturnCredentialsBoundToImage(@Nullable @Named("image") LoginCredentials creds,
|
||||||
|
Map<String, Credentials> credentialStore) {
|
||||||
this.creds = creds;
|
this.creds = creds;
|
||||||
|
this.credentialStore = credentialStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -50,6 +55,8 @@ public class ReturnCredentialsBoundToImage implements PopulateDefaultLoginCreden
|
||||||
if (creds != null)
|
if (creds != null)
|
||||||
return creds;
|
return creds;
|
||||||
Image image = Image.class.cast(resourceToAuthenticate);
|
Image image = Image.class.cast(resourceToAuthenticate);
|
||||||
|
if (credentialStore.containsKey("image#" + image.getId()))
|
||||||
|
return LoginCredentials.fromCredentials(credentialStore.get("image#" + image.getId()));
|
||||||
if (image.getOperatingSystem() != null && OsFamily.WINDOWS.equals(image.getOperatingSystem().getFamily())) {
|
if (image.getOperatingSystem() != null && OsFamily.WINDOWS.equals(image.getOperatingSystem().getFamily())) {
|
||||||
return LoginCredentials.builder().user("Administrator").build();
|
return LoginCredentials.builder().user("Administrator").build();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -31,6 +31,8 @@ import org.jclouds.domain.Credentials;
|
||||||
import org.jclouds.domain.LoginCredentials;
|
import org.jclouds.domain.LoginCredentials;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
|
@ -43,7 +45,21 @@ public class ReturnCredentialsBoundToImageTest {
|
||||||
replay(image);
|
replay(image);
|
||||||
|
|
||||||
LoginCredentials creds = new LoginCredentials("ubuntu", "foo", null, false);
|
LoginCredentials creds = new LoginCredentials("ubuntu", "foo", null, false);
|
||||||
assertEquals(new ReturnCredentialsBoundToImage(creds).execute(image), creds);
|
assertEquals(new ReturnCredentialsBoundToImage(creds, ImmutableMap.<String, Credentials> of()).execute(image),
|
||||||
|
creds);
|
||||||
|
|
||||||
|
verify(image);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testDefaultIsToReturnConfiguredCredentialInStore() {
|
||||||
|
Image image = createMock(Image.class);
|
||||||
|
expect(image.getId()).andReturn("1").times(2);
|
||||||
|
replay(image);
|
||||||
|
|
||||||
|
LoginCredentials creds = new LoginCredentials("ubuntu", "foo", null, false);
|
||||||
|
assertEquals(new ReturnCredentialsBoundToImage(null, ImmutableMap.<String, Credentials> of("image#1",creds)).execute(image),
|
||||||
|
creds);
|
||||||
|
|
||||||
verify(image);
|
verify(image);
|
||||||
|
|
||||||
|
@ -51,12 +67,13 @@ public class ReturnCredentialsBoundToImageTest {
|
||||||
|
|
||||||
public void testReturnAdministratorOnWindows() {
|
public void testReturnAdministratorOnWindows() {
|
||||||
Image image = createMock(Image.class);
|
Image image = createMock(Image.class);
|
||||||
|
expect(image.getId()).andReturn("1");
|
||||||
expect(image.getOperatingSystem()).andReturn(
|
expect(image.getOperatingSystem()).andReturn(
|
||||||
OperatingSystem.builder().family(OsFamily.WINDOWS).description("foo").build()).atLeastOnce();
|
OperatingSystem.builder().family(OsFamily.WINDOWS).description("foo").build()).atLeastOnce();
|
||||||
replay(image);
|
replay(image);
|
||||||
|
|
||||||
Credentials creds = new Credentials("Administrator", null);
|
Credentials creds = new Credentials("Administrator", null);
|
||||||
assertEquals(new ReturnCredentialsBoundToImage(null).execute(image), creds);
|
assertEquals(new ReturnCredentialsBoundToImage(null, ImmutableMap.<String, Credentials> of()).execute(image), creds);
|
||||||
|
|
||||||
verify(image);
|
verify(image);
|
||||||
|
|
||||||
|
@ -64,12 +81,13 @@ public class ReturnCredentialsBoundToImageTest {
|
||||||
|
|
||||||
public void testReturnRootWhenNotOnWindows() {
|
public void testReturnRootWhenNotOnWindows() {
|
||||||
Image image = createMock(Image.class);
|
Image image = createMock(Image.class);
|
||||||
|
expect(image.getId()).andReturn("1");
|
||||||
expect(image.getOperatingSystem()).andReturn(
|
expect(image.getOperatingSystem()).andReturn(
|
||||||
OperatingSystem.builder().family(OsFamily.LINUX).description("foo").build()).atLeastOnce();
|
OperatingSystem.builder().family(OsFamily.LINUX).description("foo").build()).atLeastOnce();
|
||||||
replay(image);
|
replay(image);
|
||||||
|
|
||||||
Credentials creds = new Credentials("root", null);
|
Credentials creds = new Credentials("root", null);
|
||||||
assertEquals(new ReturnCredentialsBoundToImage(null).execute(image), creds);
|
assertEquals(new ReturnCredentialsBoundToImage(null, ImmutableMap.<String, Credentials> of()).execute(image), creds);
|
||||||
|
|
||||||
verify(image);
|
verify(image);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue