Skip to content

Commit 355206b

Browse files
dkocherchenkins
authored andcommitted
Refactor to allow storage session to allow usage of Hub API features.
1 parent 298a7b5 commit 355206b

File tree

7 files changed

+38
-55
lines changed

7 files changed

+38
-55
lines changed

hub/src/main/java/cloud/katta/protocols/hub/HubSession.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import ch.cyberduck.core.oauth.OAuth2AuthorizationService;
2828
import ch.cyberduck.core.oauth.OAuth2ErrorResponseInterceptor;
2929
import ch.cyberduck.core.oauth.OAuth2RequestInterceptor;
30-
import ch.cyberduck.core.preferences.HostPreferences;
3130
import ch.cyberduck.core.preferences.PreferencesFactory;
3231
import ch.cyberduck.core.proxy.ProxyFinder;
3332
import ch.cyberduck.core.ssl.X509KeyManager;
@@ -75,8 +74,6 @@ public class HubSession extends HttpSession<HubApiClient> {
7574

7675
private HubVaultListService vaults;
7776

78-
public static final String SKIP_LISTING_UPON_LOGIN = "skip.listing.upon.login";
79-
8077
/**
8178
* Interceptor for OpenID connect flow
8279
*/
@@ -166,10 +163,7 @@ public void login(final LoginCallback prompt, final CancelCallback cancel) throw
166163
final OAuthTokens tokens = new OAuthTokens(credentials.getOauth().getAccessToken(), credentials.getOauth().getRefreshToken(), credentials.getOauth().getExpiryInMilliseconds(),
167164
credentials.getOauth().getIdToken());
168165
vaults = new HubVaultListService(protocols, this, trust, key, registry, tokens);
169-
170-
if(!new HostPreferences(host).getBoolean(SKIP_LISTING_UPON_LOGIN)) {
171-
vaults.list(Home.root(), new DisabledListProgressListener());
172-
}
166+
vaults.list(Home.root(), new DisabledListProgressListener());
173167
}
174168
catch(SecurityFailure e) {
175169
throw new InteroperabilityException(LocaleFactory.localizedString("Login failed", "Credentials"), e);

hub/src/main/java/cloud/katta/protocols/hub/HubVaultListService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public AttributedList<Path> list(final Path directory, final ListProgressListene
9696
}
9797
throw e;
9898
}
99-
final Host bookmark = vaultService.getStorageBackend(protocols, configDto, vaultDto.getId(),
99+
final Host bookmark = vaultService.getStorageBackend(protocols, session, configDto, vaultDto.getId(),
100100
vaultMetadata.storage(), tokens);
101101
log.debug("Configured {} for vault {}", bookmark, vaultDto);
102102
final Session<?> storage = SessionFactory.create(bookmark, trust, key);

hub/src/main/java/cloud/katta/protocols/s3/TokenExchangeRequestInterceptor.java

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,15 @@
55
package cloud.katta.protocols.s3;
66

77
import ch.cyberduck.core.Credentials;
8-
import ch.cyberduck.core.DisabledCancelCallback;
9-
import ch.cyberduck.core.DisabledHostKeyCallback;
10-
import ch.cyberduck.core.DisabledLoginCallback;
11-
import ch.cyberduck.core.DisabledPasswordCallback;
12-
import ch.cyberduck.core.DisabledProgressListener;
138
import ch.cyberduck.core.Host;
14-
import ch.cyberduck.core.HostParser;
159
import ch.cyberduck.core.LoginCallback;
16-
import ch.cyberduck.core.LoginConnectionService;
1710
import ch.cyberduck.core.OAuthTokens;
18-
import ch.cyberduck.core.PasswordStoreFactory;
19-
import ch.cyberduck.core.ProtocolFactory;
20-
import ch.cyberduck.core.SessionFactory;
2111
import ch.cyberduck.core.exception.BackgroundException;
2212
import ch.cyberduck.core.exception.LoginCanceledException;
2313
import ch.cyberduck.core.exception.LoginFailureException;
2414
import ch.cyberduck.core.oauth.OAuth2RequestInterceptor;
2515
import ch.cyberduck.core.preferences.HostPreferences;
2616
import ch.cyberduck.core.preferences.PreferencesReader;
27-
import ch.cyberduck.core.ssl.DefaultX509KeyManager;
28-
import ch.cyberduck.core.ssl.DefaultX509TrustManager;
29-
import ch.cyberduck.core.vault.VaultRegistryFactory;
30-
31-
import static cloud.katta.protocols.hub.HubSession.SKIP_LISTING_UPON_LOGIN;
3217

3318
import org.apache.http.client.HttpClient;
3419
import org.apache.logging.log4j.LogManager;
@@ -37,7 +22,6 @@
3722
import java.util.Arrays;
3823
import java.util.List;
3924

40-
import cloud.katta.client.ApiClient;
4125
import cloud.katta.client.ApiException;
4226
import cloud.katta.client.api.StorageResourceApi;
4327
import cloud.katta.client.model.AccessTokenResponse;
@@ -47,8 +31,6 @@
4731
import com.auth0.jwt.exceptions.JWTDecodeException;
4832
import com.auth0.jwt.interfaces.DecodedJWT;
4933

50-
import static cloud.katta.protocols.s3.S3AssumeRoleProtocol.OAUTH_TOKENEXCHANGE_BASEPATH;
51-
5234
/**
5335
* Exchange OIDC token to scoped token using OAuth 2.0 Token Exchange. Used for S3-STS in Katta.
5436
*/
@@ -61,14 +43,11 @@ public class TokenExchangeRequestInterceptor extends OAuth2RequestInterceptor {
6143
*/
6244
public static final String OIDC_AUTHORIZED_PARTY = "azp";
6345

64-
6546
private final Host bookmark;
66-
private final HttpClient client;
6747

6848
public TokenExchangeRequestInterceptor(final HttpClient client, final Host bookmark, final LoginCallback prompt) throws LoginCanceledException {
6949
super(client, bookmark, prompt);
7050
this.bookmark = bookmark;
71-
this.client = client;
7251
}
7352

7453
@Override
@@ -92,9 +71,9 @@ public OAuthTokens refresh(final OAuthTokens previous) throws BackgroundExceptio
9271
public OAuthTokens exchange(final OAuthTokens previous) throws BackgroundException {
9372
log.info("Exchange tokens {} for {}", previous, bookmark);
9473
final PreferencesReader preferences = new HostPreferences(bookmark);
95-
final ApiClient apiClient = getHubApiClient(previous, preferences);
96-
97-
final StorageResourceApi api = new StorageResourceApi(apiClient);
74+
final HubSession hub = bookmark.getProtocol().getFeature(HubSession.class);
75+
log.debug("Exchange token with hub {}", hub);
76+
final StorageResourceApi api = new StorageResourceApi(hub.getClient());
9877
try {
9978
AccessTokenResponse tokenExchangeResponse = api.apiStorageS3TokenPost(preferences.getProperty(S3AssumeRoleProtocol.OAUTH_TOKENEXCHANGE_VAULT));
10079
// N.B. token exchange with Id token does not work!
@@ -109,18 +88,6 @@ public OAuthTokens exchange(final OAuthTokens previous) throws BackgroundExcepti
10988
}
11089
}
11190

112-
private static ApiClient getHubApiClient(final OAuthTokens previous, final PreferencesReader preferences) throws BackgroundException {
113-
final ProtocolFactory factory = ProtocolFactory.get();
114-
final Host hub = new HostParser(factory).get(preferences.getProperty(OAUTH_TOKENEXCHANGE_BASEPATH)).withCredentials(new Credentials().withOauth(new OAuthTokens(previous)));
115-
hub.setProperty(SKIP_LISTING_UPON_LOGIN, "true"); // prevent infinite recursion
116-
final HubSession session = (HubSession) SessionFactory.create(hub, new DefaultX509TrustManager(), new DefaultX509KeyManager())
117-
.withRegistry(VaultRegistryFactory.get(new DisabledPasswordCallback()));
118-
final LoginConnectionService login = new LoginConnectionService(new DisabledLoginCallback(), new DisabledHostKeyCallback(),
119-
PasswordStoreFactory.get(), new DisabledProgressListener());
120-
login.check(session, new DisabledCancelCallback());
121-
return session.getClient();
122-
}
123-
12491
@Override
12592
public Credentials validate() throws BackgroundException {
12693
final Credentials credentials = super.validate();

hub/src/main/java/cloud/katta/workflows/CreateVaultService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void createVault(final UserKeys userKeys, final StorageProfileDtoWrapper
134134

135135
final OAuthTokens tokens = keychain.findOAuthTokens(hubSession.getHost());
136136
final Host bookmark = new VaultServiceImpl(vaultResource, storageProfileResource).getStorageBackend(ProtocolFactory.get(),
137-
configResource.apiConfigGet(), vaultDto.getId(), metadataPayload.storage(), tokens);
137+
hubSession, configResource.apiConfigGet(), vaultDto.getId(), metadataPayload.storage(), tokens);
138138
if(storageProfileWrapper.getProtocol() == Protocol.S3) {
139139
// permanent: template upload into existing bucket from client (not backend)
140140
templateUploadService.uploadTemplate(bookmark, metadataPayload, storageDto, hashedRootDirId);

hub/src/main/java/cloud/katta/workflows/VaultService.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
package cloud.katta.workflows;
66

77
import ch.cyberduck.core.Host;
8-
9-
import java.util.UUID;
10-
118
import ch.cyberduck.core.OAuthTokens;
129
import ch.cyberduck.core.ProtocolFactory;
1310

11+
import java.util.UUID;
12+
1413
import cloud.katta.client.ApiException;
1514
import cloud.katta.client.model.ConfigDto;
1615
import cloud.katta.crypto.UserKeys;
1716
import cloud.katta.crypto.uvf.UvfAccessTokenPayload;
1817
import cloud.katta.crypto.uvf.UvfMetadataPayload;
1918
import cloud.katta.crypto.uvf.VaultMetadataJWEBackendDto;
19+
import cloud.katta.protocols.hub.HubSession;
2020
import cloud.katta.workflows.exceptions.AccessException;
2121
import cloud.katta.workflows.exceptions.SecurityFailure;
2222

@@ -47,13 +47,15 @@ public interface VaultService {
4747

4848
/**
4949
* Prepares (virtual) bookmark for vault to access its configured storage backend.
50+
*
5051
* @param protocols Registered protocol implementations to access backend storage
52+
* @param hub Hub API Connection
5153
* @param configDto Hub configuration
52-
* @param vaultId Vault ID
53-
* @param metadata Storage Backend configuration
54+
* @param vaultId Vault ID
55+
* @param metadata Storage Backend configuration
5456
* @return Configuration
5557
* @throws AccessException Unsupported backend storage protocol
56-
* @throws ApiException Server error accessing storage profile
58+
* @throws ApiException Server error accessing storage profile
5759
*/
58-
Host getStorageBackend(final ProtocolFactory protocols, final ConfigDto configDto, UUID vaultId, VaultMetadataJWEBackendDto metadata, final OAuthTokens tokens) throws AccessException, ApiException;
60+
Host getStorageBackend(final ProtocolFactory protocols, final HubSession hub, final ConfigDto configDto, UUID vaultId, VaultMetadataJWEBackendDto metadata, final OAuthTokens tokens) throws AccessException, ApiException;
5961
}

hub/src/main/java/cloud/katta/workflows/VaultServiceImpl.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public UvfAccessTokenPayload getVaultAccessTokenJWE(final UUID vaultId, final Us
8484
}
8585

8686
@Override
87-
public Host getStorageBackend(final ProtocolFactory protocols, final ConfigDto configDto, final UUID vaultId, final VaultMetadataJWEBackendDto vaultMetadata, final OAuthTokens tokens) throws ApiException, AccessException {
87+
public Host getStorageBackend(final ProtocolFactory protocols, final HubSession hub, final ConfigDto configDto, final UUID vaultId, final VaultMetadataJWEBackendDto vaultMetadata, final OAuthTokens tokens) throws ApiException, AccessException {
8888
if(null == protocols.forName(vaultMetadata.getProvider())) {
8989
log.debug("Load missing profile {}", vaultMetadata.getProvider());
9090
final StorageProfileDtoWrapper storageProfile = StorageProfileDtoWrapper.coerce(storageProfileResourceApi
@@ -93,8 +93,8 @@ public Host getStorageBackend(final ProtocolFactory protocols, final ConfigDto c
9393
switch(storageProfile.getProtocol()) {
9494
case S3:
9595
case S3_STS:
96-
final Profile profile = new Profile(protocols.forType(protocols.find(ProtocolFactory.BUNDLED_PROFILE_PREDICATE), Protocol.Type.s3), new StorageProfileDtoWrapperDeserializer(
97-
new HubConfigDtoDeserializer(configDto), storageProfile));
96+
final Profile profile = new HubAwareProfile(hub, protocols.forType(protocols.find(ProtocolFactory.BUNDLED_PROFILE_PREDICATE), Type.s3),
97+
configDto, storageProfile);
9898
log.debug("Register storage profile {}", profile);
9999
protocols.register(profile);
100100
break;
@@ -129,4 +129,23 @@ public Host getStorageBackend(final ProtocolFactory protocols, final ConfigDto c
129129
bookmark.setRegion(vaultMetadata.getRegion());
130130
return bookmark;
131131
}
132+
133+
private static final class HubAwareProfile extends Profile {
134+
private final HubSession hub;
135+
136+
public HubAwareProfile(final HubSession hub, final Protocol parent, final ConfigDto configDto, final StorageProfileDtoWrapper storageProfile) {
137+
super(parent, new StorageProfileDtoWrapperDeserializer(
138+
new HubConfigDtoDeserializer(configDto), storageProfile));
139+
this.hub = hub;
140+
}
141+
142+
@SuppressWarnings("unchecked")
143+
@Override
144+
public <T> T getFeature(final Class<T> type) {
145+
if(type == HubSession.class) {
146+
return (T) hub;
147+
}
148+
return super.getFeature(type);
149+
}
150+
}
132151
}

hub/src/test/java/cloud/katta/core/AbstractHubSynchronizeTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ public void test03AddVault(final HubTestConfig config) throws Exception {
238238
final Host bookmark = new VaultServiceImpl(new VaultResourceApi(hubSession.getClient()), new StorageProfileResourceApi(hubSession.getClient()))
239239
.getStorageBackend(
240240
ProtocolFactory.get(),
241+
hubSession,
241242
new ConfigResourceApi(hubSession.getClient()).apiConfigGet(), vaultId, new VaultMetadataJWEBackendDto()
242243
.provider(storageProfileWrapper.getId().toString())
243244
.defaultPath(config.vault.bucketName)

0 commit comments

Comments
 (0)