-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #906 from NASA-PDS/issue_895
Refactored `validate-refs` to use new OpenSearch Serverless and registry-common library
- Loading branch information
Showing
71 changed files
with
169 additions
and
758 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 32 additions & 93 deletions
125
src/main/java/gov/nasa/pds/validate/ri/AuthInformation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,41 @@ | ||
package gov.nasa.pds.validate.ri; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.util.Properties; | ||
import java.util.Scanner; | ||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.SAXException; | ||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.lang.NotImplementedException; | ||
import gov.nasa.pds.registry.common.ConnectionFactory; | ||
import gov.nasa.pds.registry.common.EstablishConnectionFactory; | ||
|
||
public class AuthInformation { | ||
final public static AuthInformation NO_AUTH = new AuthInformation(false, "", "", ""); | ||
final private boolean trustSelfSigned; | ||
final private String password; | ||
final private String url; | ||
final private String username; | ||
|
||
private AuthInformation(boolean tss, String pwd, String un, String url) { | ||
this.password = pwd; | ||
this.trustSelfSigned = tss; | ||
this.url = url; | ||
this.username = un; | ||
final private String apiAuthFile; | ||
final private String osAuthFile; | ||
final private String regConn; | ||
private transient ConnectionFactory factory = null; | ||
private AuthInformation(String a, String A, String r) { | ||
this.apiAuthFile = A; | ||
this.osAuthFile = a; | ||
this.regConn = r; | ||
} | ||
|
||
public static AuthInformation buildFrom(String filename) | ||
throws IOException, ParserConfigurationException, SAXException { | ||
boolean tss; | ||
File file = new File(filename); | ||
Scanner textReader; | ||
String line = null, pwd, un, url; | ||
|
||
if (filename == null || filename.length() == 0) | ||
return NO_AUTH; | ||
if (!file.exists()) | ||
throw new IOException("Filename '" + filename + "' does not exist"); | ||
|
||
// Get the first non-comment line | ||
textReader = new Scanner(file, Charset.defaultCharset().name()); | ||
while (textReader.hasNext() && line == null) { | ||
line = textReader.nextLine().strip(); | ||
if (line.charAt(0) == '#') | ||
line = null; | ||
} | ||
textReader.close(); | ||
|
||
// Determine which file processing to use | ||
if (line.startsWith("<?xml ") && line.endsWith("?>")) { // XML | ||
// <registry url="http://localhost:9200" index="registry" auth="/path/to/auth.cfg" /> | ||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); | ||
Document document = builder.parse(file); | ||
NodeList registries = document.getElementsByTagName("registry"); | ||
|
||
if (registries.getLength() != 1) | ||
throw new SAXException( | ||
"There should be one and only registry tag in the harvest config file but found " | ||
+ registries.getLength()); | ||
if (registries.item(0).getAttributes().getNamedItem("auth") == null) | ||
throw new SAXException("Requires an authorization file or 'auth' attribute on <registry>."); | ||
|
||
filename = registries.item(0).getAttributes().getNamedItem("auth").getNodeValue(); | ||
url = registries.item(0).getAttributes().getNamedItem("url").getNodeValue(); | ||
} else { // java property | ||
FileInputStream input = new FileInputStream(file); | ||
Properties properties = new Properties(); | ||
properties.load(input); | ||
url = properties.getProperty("url"); | ||
filename = properties.getProperty("credentials"); | ||
input.close(); | ||
} | ||
|
||
// Get credentials | ||
FileInputStream input = new FileInputStream(filename); | ||
Properties properties = new Properties(); | ||
properties.load(input); | ||
pwd = properties.getProperty("password"); | ||
tss = Boolean.valueOf(properties.getProperty("trust.self-signed", "false")); | ||
un = properties.getProperty("user"); | ||
input.close(); | ||
return new AuthInformation(tss, pwd, un, url); | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public boolean getTrustSelfSigned() { | ||
return trustSelfSigned; | ||
public static AuthInformation buildFrom(CommandLine cl) { | ||
return new AuthInformation( | ||
cl.getOptionValue("a",""), | ||
cl.getOptionValue("A",""), | ||
cl.getOptionValue("r","")); | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
public synchronized ConnectionFactory getConnectionFactory() throws Exception { | ||
if (this.factory == null) { | ||
if (!this.apiAuthFile.isBlank()) { | ||
throw new NotImplementedException(); | ||
} | ||
if (!this.osAuthFile.isBlank()) { | ||
this.factory = EstablishConnectionFactory.from(this.regConn, this.osAuthFile); | ||
} | ||
if (this.factory == null) { | ||
throw new IllegalArgumentException("did not supply necessary arguments on the CLI"); | ||
} | ||
} | ||
return this.factory; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
public String getURL() { | ||
return factory != null ? this.factory.toString() : "uninitialized connection factory"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.