Skip to content

Commit 1a6121b

Browse files
ylangiscdkocher
authored andcommitted
Introduce VaultMetadataProvider.
1 parent 1ab437e commit 1a6121b

File tree

10 files changed

+213
-10
lines changed

10 files changed

+213
-10
lines changed

core/src/main/java/ch/cyberduck/core/features/Vault.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import ch.cyberduck.core.exception.NotfoundException;
2424
import ch.cyberduck.core.exception.UnsupportedException;
2525
import ch.cyberduck.core.vault.DisabledVault;
26-
import ch.cyberduck.core.vault.VaultCredentials;
2726
import ch.cyberduck.core.vault.VaultMetadata;
27+
import ch.cyberduck.core.vault.VaultMetadataProvider;
2828

2929
public interface Vault {
3030

@@ -36,7 +36,8 @@ public interface Vault {
3636
* @throws BackgroundException Failure reading master key from server
3737
* @throws NotfoundException No master key file in home
3838
*/
39-
Vault create(Session<?> session, String region, VaultCredentials credentials) throws BackgroundException;
39+
40+
Vault create(Session<?> session, String region, VaultMetadataProvider metadata) throws BackgroundException;
4041

4142
/**
4243
* Open existing vault

core/src/main/java/ch/cyberduck/core/vault/DisabledVault.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import ch.cyberduck.core.PasswordCallback;
1919
import ch.cyberduck.core.Path;
2020
import ch.cyberduck.core.Session;
21+
import ch.cyberduck.core.exception.BackgroundException;
2122
import ch.cyberduck.core.features.Vault;
2223

2324
import java.util.EnumSet;
@@ -36,7 +37,7 @@ public DisabledVault(final Path home) {
3637
}
3738

3839
@Override
39-
public Vault create(final Session<?> session, final String region, final VaultCredentials credentials) {
40+
public Vault create(final Session<?> session, final String region, final VaultMetadataProvider metadata) throws BackgroundException {
4041
return null;
4142
}
4243

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ch.cyberduck.core.vault;
2+
3+
/*
4+
* Copyright (c) 2002-2025 iterate GmbH. All rights reserved.
5+
* https://cyberduck.io/
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*/
17+
18+
public interface VaultMetadataProvider {
19+
20+
//Map<Path, byte[]> metadataFiles() throws BackgroundException;
21+
22+
//String dirPath();
23+
}

cryptomator/src/main/java/ch/cyberduck/core/cryptomator/CryptoVaultProvider.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import ch.cyberduck.core.ListProgressListener;
1919
import ch.cyberduck.core.Path;
2020
import ch.cyberduck.core.Session;
21+
import ch.cyberduck.core.cryptomator.impl.uvf.DefaultVaultMetadataUVFProvider;
22+
import ch.cyberduck.core.cryptomator.impl.v8.DefaultVaultMetadataV8Provider;
2123
import ch.cyberduck.core.exception.BackgroundException;
2224
import ch.cyberduck.core.features.Find;
2325
import ch.cyberduck.core.preferences.HostPreferencesFactory;
@@ -94,9 +96,10 @@ public synchronized AbstractVault provide(final Session<?> session, final VaultM
9496
public AbstractVault create(final Session<?> session, final String region, final VaultCredentials credentials, final VaultMetadata metadata) throws BackgroundException {
9597
switch(metadata.type) {
9698
case V8:
97-
return new ch.cyberduck.core.cryptomator.impl.v8.CryptoVault(metadata.root).create(session, region, credentials);
99+
return new ch.cyberduck.core.cryptomator.impl.v8.CryptoVault(metadata.root).create(session, region, new DefaultVaultMetadataV8Provider(credentials));
98100
case UVF:
99-
return new ch.cyberduck.core.cryptomator.impl.uvf.CryptoVault(metadata.root).create(session, region, credentials);
101+
//TODO plain UVF
102+
return new ch.cyberduck.core.cryptomator.impl.uvf.CryptoVault(metadata.root).create(session, region, new DefaultVaultMetadataUVFProvider());
100103
default:
101104
log.error("Unknown vault type {}", metadata.type);
102105
// TODO schmeissen, DISABLED zurück geben geht nicht weil kein AbstractVault

cryptomator/src/main/java/ch/cyberduck/core/cryptomator/impl/uvf/CryptoVault.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import ch.cyberduck.core.Session;
2424
import ch.cyberduck.core.SimplePathPredicate;
2525
import ch.cyberduck.core.cryptomator.AbstractVault;
26+
import ch.cyberduck.core.cryptomator.ContentWriter;
2627
import ch.cyberduck.core.cryptomator.CryptoDirectory;
2728
import ch.cyberduck.core.cryptomator.CryptoFilename;
2829
import ch.cyberduck.core.cryptomator.features.CryptoDirectoryUVFFeature;
@@ -32,9 +33,13 @@
3233
import ch.cyberduck.core.exception.BackgroundException;
3334
import ch.cyberduck.core.exception.UnsupportedException;
3435
import ch.cyberduck.core.features.Directory;
36+
import ch.cyberduck.core.features.Encryption;
3537
import ch.cyberduck.core.features.Vault;
36-
import ch.cyberduck.core.vault.VaultCredentials;
38+
import ch.cyberduck.core.features.Write;
39+
import ch.cyberduck.core.preferences.PreferencesFactory;
40+
import ch.cyberduck.core.transfer.TransferStatus;
3741
import ch.cyberduck.core.vault.VaultMetadata;
42+
import ch.cyberduck.core.vault.VaultMetadataProvider;
3843

3944
import org.apache.logging.log4j.LogManager;
4045
import org.apache.logging.log4j.Logger;
@@ -83,8 +88,37 @@ public CryptoVault(final Path home) {
8388
}
8489

8590
@Override
86-
public AbstractVault create(final Session<?> session, final String region, final VaultCredentials credentials) throws BackgroundException {
87-
throw new UnsupportedOperationException();
91+
public AbstractVault create(final Session<?> session, final String region, final VaultMetadataProvider metadata) throws BackgroundException {
92+
final VaultMetadataUVFProvider provider = VaultMetadataUVFProvider.cast(metadata);
93+
94+
final Path home = this.getHome();
95+
log.debug("Create vault root directory at {}", home);
96+
97+
// Obtain non encrypted directory writer
98+
final Directory<?> directory = session._getFeature(Directory.class);
99+
final TransferStatus status = new TransferStatus().setRegion(region);
100+
final Encryption encryption = session._getFeature(Encryption.class);
101+
if(encryption != null) {
102+
status.setEncryption(encryption.getDefault(home));
103+
}
104+
final Path vault = directory.mkdir(session._getFeature(Write.class), home, status);
105+
106+
final Path dataDir = new Path(vault, "d", EnumSet.of(Path.Type.directory));
107+
final Path firstLevel = new Path(dataDir, provider.getDirPath().substring(0, 2), EnumSet.of(Path.Type.directory));
108+
final Path secondLevel = new Path(firstLevel, provider.getDirPath().substring(2), EnumSet.of(Path.Type.directory));
109+
110+
directory.mkdir(session._getFeature(Write.class), dataDir, status);
111+
directory.mkdir(session._getFeature(Write.class), firstLevel, status);
112+
directory.mkdir(session._getFeature(Write.class), secondLevel, status);
113+
114+
// vault.uvf
115+
new ContentWriter(session).write(new Path(home, PreferencesFactory.get().getProperty("cryptomator.vault.config.filename"),
116+
EnumSet.of(Path.Type.file, Path.Type.vault)), provider.getMetadata());
117+
// dir.uvf
118+
new ContentWriter(session).write(new Path(secondLevel, "dir.uvf", EnumSet.of(Path.Type.file)),
119+
provider.getRootDirectoryMetadata());
120+
121+
return this;
88122
}
89123

90124
// load -> unlock -> open
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ch.cyberduck.core.cryptomator.impl.uvf;
2+
3+
/*
4+
* Copyright (c) 2002-2025 iterate GmbH. All rights reserved.
5+
* https://cyberduck.io/
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*/
17+
18+
public class DefaultVaultMetadataUVFProvider implements VaultMetadataUVFProvider {
19+
20+
@Override
21+
public byte[] getMetadata() {
22+
return new byte[0];
23+
}
24+
25+
@Override
26+
public byte[] getRootDirectoryMetadata() {
27+
return new byte[0];
28+
}
29+
30+
@Override
31+
public String getDirPath() {
32+
return "";
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ch.cyberduck.core.cryptomator.impl.uvf;
2+
3+
/*
4+
* Copyright (c) 2002-2025 iterate GmbH. All rights reserved.
5+
* https://cyberduck.io/
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*/
17+
18+
import ch.cyberduck.core.vault.VaultMetadataProvider;
19+
20+
public interface VaultMetadataUVFProvider extends VaultMetadataProvider {
21+
22+
byte[] getMetadata();
23+
24+
byte[] getRootDirectoryMetadata();
25+
26+
String getDirPath();
27+
28+
static VaultMetadataUVFProvider cast(VaultMetadataProvider provider) {
29+
if(provider instanceof VaultMetadataUVFProvider) {
30+
return (VaultMetadataUVFProvider) provider;
31+
}
32+
else {
33+
throw new IllegalArgumentException("Unsupported metadata type " + provider.getClass());
34+
}
35+
}
36+
}

cryptomator/src/main/java/ch/cyberduck/core/cryptomator/impl/v8/CryptoVault.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import ch.cyberduck.core.exception.NotfoundException;
4343
import ch.cyberduck.core.features.Directory;
4444
import ch.cyberduck.core.features.Encryption;
45-
import ch.cyberduck.core.features.Write;
4645
import ch.cyberduck.core.features.Vault;
4746
import ch.cyberduck.core.features.Write;
4847
import ch.cyberduck.core.preferences.Preferences;
@@ -52,6 +51,7 @@
5251
import ch.cyberduck.core.vault.VaultCredentials;
5352
import ch.cyberduck.core.vault.VaultException;
5453
import ch.cyberduck.core.vault.VaultMetadata;
54+
import ch.cyberduck.core.vault.VaultMetadataProvider;
5555

5656
import org.apache.logging.log4j.LogManager;
5757
import org.apache.logging.log4j.Logger;
@@ -212,8 +212,14 @@ public Pattern getFilenamePattern() {
212212
return FILENAME_PATTERN;
213213
}
214214

215+
public AbstractVault create(final Session<?> session, final String region, final VaultCredentials credentials) throws BackgroundException {
216+
return this.create(session, region, new DefaultVaultMetadataV8Provider(credentials));
217+
}
218+
215219
@Override
216-
public synchronized AbstractVault create(final Session<?> session, final String region, final VaultCredentials credentials) throws BackgroundException {
220+
public AbstractVault create(final Session<?> session, final String region, final VaultMetadataProvider metadata) throws BackgroundException {
221+
final VaultMetadataV8Provider provider = VaultMetadataV8Provider.cast(metadata);
222+
final VaultCredentials credentials = provider.getCredentials();
217223
final Host bookmark = session.getHost();
218224
if(credentials.isSaved()) {
219225
try {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ch.cyberduck.core.cryptomator.impl.v8;
2+
3+
/*
4+
* Copyright (c) 2002-2025 iterate GmbH. All rights reserved.
5+
* https://cyberduck.io/
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*/
17+
18+
import ch.cyberduck.core.vault.VaultCredentials;
19+
20+
public class DefaultVaultMetadataV8Provider implements VaultMetadataV8Provider {
21+
22+
private VaultCredentials credentials;
23+
24+
public DefaultVaultMetadataV8Provider(final VaultCredentials credentials) {
25+
this.credentials = credentials;
26+
}
27+
28+
@Override
29+
public VaultCredentials getCredentials() {
30+
return credentials;
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ch.cyberduck.core.cryptomator.impl.v8;
2+
3+
/*
4+
* Copyright (c) 2002-2025 iterate GmbH. All rights reserved.
5+
* https://cyberduck.io/
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*/
17+
18+
import ch.cyberduck.core.vault.VaultCredentials;
19+
import ch.cyberduck.core.vault.VaultMetadataProvider;
20+
21+
public interface VaultMetadataV8Provider extends VaultMetadataProvider {
22+
23+
VaultCredentials getCredentials();
24+
25+
static VaultMetadataV8Provider cast(VaultMetadataProvider provider) {
26+
if(provider instanceof VaultMetadataV8Provider) {
27+
return (VaultMetadataV8Provider) provider;
28+
}
29+
else {
30+
throw new IllegalArgumentException("Unsupported metadata type " + provider.getClass());
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)