Skip to content

Commit

Permalink
ProtocolsGenerator ctor change
Browse files Browse the repository at this point in the history
changes key files to an already instantiated TokenTool
  • Loading branch information
pdowler committed Dec 12, 2023
1 parent a66a21d commit 1db9f37
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ public class ProtocolsGenerator {
private final ArtifactDAO artifactDAO;
private final DeletedArtifactEventDAO deletedArtifactEventDAO;
private final String user;
private final File publicKeyFile;
private final File privateKeyFile;
private final TokenTool tokenGen;
private final Map<URI, Availability> siteAvailabilities;
private final Map<URI, StorageSiteRule> siteRules;
private final StorageResolver storageResolver;
Expand All @@ -135,14 +134,13 @@ public class ProtocolsGenerator {
boolean storageResolverAdded = false;


public ProtocolsGenerator(ArtifactDAO artifactDAO, File publicKeyFile, File privateKeyFile, String user,
public ProtocolsGenerator(ArtifactDAO artifactDAO, TokenTool tokenGen, String user,
Map<URI, Availability> siteAvailabilities, Map<URI, StorageSiteRule> siteRules,
boolean preventNotFound, StorageResolver storageResolver) {
this.artifactDAO = artifactDAO;
this.deletedArtifactEventDAO = new DeletedArtifactEventDAO(this.artifactDAO);
this.user = user;
this.publicKeyFile = publicKeyFile;
this.privateKeyFile = privateKeyFile;
this.tokenGen = tokenGen;
this.siteAvailabilities = siteAvailabilities;
this.siteRules = siteRules;
this.preventNotFound = preventNotFound;
Expand All @@ -156,13 +154,12 @@ public boolean getStorageResolverAdded() {
public List<Protocol> getProtocols(Transfer transfer) throws ResourceNotFoundException, IOException {
String authToken = null;
URI artifactURI = transfer.getTargets().get(0); // see PostAction line ~127
if (publicKeyFile != null && privateKeyFile != null) {
if (tokenGen != null) {
// create an auth token
TokenTool tk = new TokenTool(publicKeyFile, privateKeyFile);
if (transfer.getDirection().equals(Direction.pullFromVoSpace)) {
authToken = tk.generateToken(artifactURI, ReadGrant.class, user);
authToken = tokenGen.generateToken(artifactURI, ReadGrant.class, user);
} else {
authToken = tk.generateToken(artifactURI, WriteGrant.class, user);
authToken = tokenGen.generateToken(artifactURI, WriteGrant.class, user);
}
}

Expand Down
15 changes: 7 additions & 8 deletions raven/src/main/java/org/opencadc/raven/ArtifactAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import org.opencadc.inventory.db.ArtifactDAO;
import org.opencadc.inventory.transfer.StorageSiteRule;
import org.opencadc.permissions.ReadGrant;
import org.opencadc.permissions.TokenTool;
import org.opencadc.permissions.WriteGrant;
import org.opencadc.permissions.client.PermissionsCheck;
import org.opencadc.vospace.transfer.Direction;
Expand All @@ -110,8 +111,7 @@ public abstract class ArtifactAction extends RestAction {

// immutable state set in constructor
protected final ArtifactDAO artifactDAO;
protected final File publicKeyFile;
protected final File privateKeyFile;
protected final TokenTool tokenGen;
protected final List<URI> readGrantServices = new ArrayList<>();
protected final List<URI> writeGrantServices = new ArrayList<>();
protected StorageResolver storageResolver;
Expand All @@ -126,8 +126,7 @@ public abstract class ArtifactAction extends RestAction {
ArtifactAction(boolean init) {
super();
this.authenticateOnly = false;
this.publicKeyFile = null;
this.privateKeyFile = null;
this.tokenGen = null;
this.artifactDAO = null;
this.preventNotFound = false;
this.storageResolver = null;
Expand Down Expand Up @@ -184,14 +183,14 @@ protected ArtifactAction() {
String privkeyFileName = props.getFirstPropertyValue(RavenInitAction.PRIVKEYFILE_KEY);
if (pubkeyFileName == null && privkeyFileName == null) {
log.debug("public/private key preauth not enabled by config");
this.publicKeyFile = null;
this.privateKeyFile = null;
this.tokenGen = null;
} else {
this.publicKeyFile = new File(System.getProperty("user.home") + "/config/" + pubkeyFileName);
this.privateKeyFile = new File(System.getProperty("user.home") + "/config/" + privkeyFileName);
File publicKeyFile = new File(System.getProperty("user.home") + "/config/" + pubkeyFileName);
File privateKeyFile = new File(System.getProperty("user.home") + "/config/" + privkeyFileName);
if (!publicKeyFile.exists() || !privateKeyFile.exists()) {
throw new IllegalStateException("invalid config: missing public/private key pair files -- " + publicKeyFile + " | " + privateKeyFile);
}
this.tokenGen = new TokenTool(publicKeyFile, privateKeyFile);
}

Map<String, Object> config = RavenInitAction.getDaoConfig(props);
Expand Down
2 changes: 1 addition & 1 deletion raven/src/main/java/org/opencadc/raven/GetFilesAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ URI getFirstURL() throws ResourceNotFoundException, IOException {
proto.setSecurityMethod(Standards.SECURITY_METHOD_ANON);
transfer.getProtocols().add(proto);

ProtocolsGenerator pg = new ProtocolsGenerator(this.artifactDAO, this.publicKeyFile, this.privateKeyFile,
ProtocolsGenerator pg = new ProtocolsGenerator(this.artifactDAO, this.tokenGen,
this.user, this.siteAvailabilities, this.siteRules,
this.preventNotFound, this.storageResolver);
List<Protocol> protos = pg.getProtocols(transfer);
Expand Down
6 changes: 3 additions & 3 deletions raven/src/main/java/org/opencadc/raven/HeadFilesAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ public void doAction() throws Exception {
if (artifact == null) {
if (this.preventNotFound) {
// check known storage sites
ProtocolsGenerator pg = new ProtocolsGenerator(this.artifactDAO, this.publicKeyFile, this.privateKeyFile,
ProtocolsGenerator pg = new ProtocolsGenerator(this.artifactDAO, this.tokenGen,
this.user, this.siteAvailabilities, this.siteRules, this.preventNotFound, this.storageResolver);
StorageSiteDAO storageSiteDAO = new StorageSiteDAO(artifactDAO);
Transfer transfer = new Transfer(artifactURI, Direction.pullFromVoSpace);
Protocol proto = new Protocol(VOS.PROTOCOL_HTTPS_GET);
proto.setSecurityMethod(Standards.SECURITY_METHOD_ANON);
transfer.getProtocols().add(proto);
TokenTool tk = new TokenTool(publicKeyFile, privateKeyFile);
String authToken = tk.generateToken(artifactURI, ReadGrant.class, user);
// TODO: tokenGen is optional so this can fail
String authToken = tokenGen.generateToken(artifactURI, ReadGrant.class, user);
artifact = pg.getUnsyncedArtifact(artifactURI, transfer, storageSiteDAO.list(), authToken);
}
}
Expand Down
2 changes: 1 addition & 1 deletion raven/src/main/java/org/opencadc/raven/PostAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public InlineContentHandler.Content accept(String name, String contentType, Inpu
public void doAction() throws Exception {
initAndAuthorize();

ProtocolsGenerator pg = new ProtocolsGenerator(this.artifactDAO, this.publicKeyFile, this.privateKeyFile,
ProtocolsGenerator pg = new ProtocolsGenerator(this.artifactDAO, this.tokenGen,
this.user, this.siteAvailabilities, this.siteRules,
this.preventNotFound, this.storageResolver);
Transfer ret = new Transfer(artifactURI, transfer.getDirection());
Expand Down

0 comments on commit 1db9f37

Please sign in to comment.