Search in sources :

Example 11 with Md5Hash

use of com.fathomdb.hash.Md5Hash in project platformlayer by platformlayer.

the class CryptoUtils method md5.

public static Md5Hash md5(byte[] b1, byte[] b2) {
    MessageDigest digest = buildMd5();
    digest.update(b1);
    byte[] hash = digest.digest(b2);
    return new Md5Hash(hash);
}
Also used : MessageDigest(java.security.MessageDigest) Md5Hash(com.fathomdb.hash.Md5Hash)

Example 12 with Md5Hash

use of com.fathomdb.hash.Md5Hash in project platformlayer by platformlayer.

the class SecretStore method findChallengeForCertificate.

// public CryptoKey getSecretFromToken(final int tokenId, final byte[] tokenSecret) {
// SecretStoreDecoder visitor = new SecretStoreDecoder() {
// @Override
// public void visitToken(int itemTokenId, byte[] itemData) {
// if (itemTokenId == tokenId) {
// // We want this to be reversible; so we XOR it.
// // Both keys are random, so this is secure
// if (tokenSecret.length == itemData.length) {
// byte[] key = SecretStore.xorByteArrays(itemData, tokenSecret);
// setSecretKey(FathomdbCrypto.deserializeKey(key));
// }
// }
// }
// };
//
// try {
// read(encoded, visitor);
// } catch (IOException e) {
// throw new IllegalArgumentException("Secret data is corrupted", e);
// }
// return visitor.getSecretKey();
// }
// static class TokenSecretFinder extends SecretStoreVisitor {
// final int tokenId;
// final CryptoKey userSecret;
//
// public TokenSecretFinder(int tokenId, CryptoKey userSecret) {
// this.tokenId = tokenId;
// this.userSecret = userSecret;
// }
//
// public byte[] tokenSecret;
//
// @Override
// public void visitToken(int tokenId, byte[] data) {
// if (tokenId == this.tokenId) {
// byte[] userSecretBytes = FathomdbCrypto.serialize(userSecret);
// this.tokenSecret = SecretStore.xorByteArrays(userSecretBytes, data);
// }
// }
// }
// public byte[] getTokenSecretWithUserSecret(int tokenId, CryptoKey userSecret) {
// TokenSecretFinder visitor = new TokenSecretFinder(tokenId, userSecret);
//
// try {
// read(encoded, visitor);
// } catch (IOException e) {
// throw new IllegalArgumentException("Secret data is corrupted", e);
// }
// return visitor.tokenSecret;
// }
public byte[] findChallengeForCertificate(X509Certificate[] certificateChain) {
    if (certificateChain.length == 0) {
        throw new IllegalArgumentException();
    }
    X509Certificate certificate = certificateChain[0];
    Md5Hash signature = OpenSshUtils.getSignature(certificate.getPublicKey());
    final byte[] signatureBytes = signature.toByteArray();
    SecretStoreDecoder visitor = new SecretStoreDecoder() {

        @Override
        public void visitGenericAsymetricKey(byte[] publicKeySignature, byte[] data) {
            if (Arrays.equals(publicKeySignature, signatureBytes)) {
                result = data;
            }
        }
    };
    try {
        read(encoded, visitor);
    } catch (IOException e) {
        throw new IllegalArgumentException("Secret data is corrupted", e);
    }
    return (byte[]) visitor.result;
}
Also used : IOException(java.io.IOException) Md5Hash(com.fathomdb.hash.Md5Hash) X509Certificate(java.security.cert.X509Certificate)

Example 13 with Md5Hash

use of com.fathomdb.hash.Md5Hash in project platformlayer by platformlayer.

the class DownloadFileByHash method uploadFile.

@Override
protected void uploadFile(OpsTarget target, File remoteFilePath) throws IOException, OpsException {
    target.mkdir(remoteFilePath.getParentFile());
    Md5Hash resolved = getResolved(target);
    CasStoreObject casObject;
    CasStoreMap casStoreMap = cas.getCasStoreMap(target);
    try {
        casObject = casStoreMap.findArtifact(new OpsCasTarget(target), resolved);
    } catch (Exception e) {
        throw new OpsException("Error while resolving artifact:" + getHumanName(), e);
    }
    if (url != null && casObject == null) {
        target.mkdir(remoteFilePath.getParentFile());
        CurlRequest curlRequest = new CurlRequest(url);
        curlRequest.bareRequest = true;
        CommandEnvironment commandEnvironment = httpProxies.getHttpProxyEnvironment(target, Usage.General, url);
        Command curlCommand = curlRequest.toCommand();
        curlCommand.addLiteral(">");
        curlCommand.addFile(remoteFilePath);
        curlCommand.setEnvironment(commandEnvironment);
        curlCommand.setTimeout(TimeSpan.FIVE_MINUTES);
        // TODO: Can we cache into CAS instead??
        log.info("Not found in CAS system; downloading directly: " + url);
        target.executeCommand(curlCommand);
    } else {
        if (casObject == null) {
            throw new OpsException("Unable to find artifact: " + getHumanName());
        }
        log.info("Doing a CAS copy from " + casObject + " to target");
        cas.copyObject(casStoreMap, casObject, new OpsCasTarget(target), remoteFilePath, true);
    }
}
Also used : CasStoreMap(org.platformlayer.cas.CasStoreMap) OpsException(org.platformlayer.ops.OpsException) CasStoreObject(org.platformlayer.cas.CasStoreObject) Command(org.platformlayer.ops.Command) CurlRequest(org.platformlayer.ops.helpers.CurlRequest) CommandEnvironment(org.platformlayer.ops.CommandEnvironment) Md5Hash(com.fathomdb.hash.Md5Hash) OpsCasTarget(org.platformlayer.ops.cas.OpsCasTarget) URISyntaxException(java.net.URISyntaxException) OpsException(org.platformlayer.ops.OpsException) IOException(java.io.IOException)

Example 14 with Md5Hash

use of com.fathomdb.hash.Md5Hash in project platformlayer by platformlayer.

the class FilesystemCasStore method checkDirectory.

private File checkDirectory(File base, Md5Hash hash, int splits) throws OpsException {
    String relativePath = toRelativePath(hash, splits);
    File seedFile = new File(base, relativePath);
    FilesystemInfo seedFileInfo = host.getFilesystemInfoFile(seedFile);
    if (seedFileInfo != null) {
        Md5Hash seedFileHash = host.getFileHash(seedFile);
        if (!seedFileHash.equals(hash)) {
            log.warn("Hash mismatch on file: " + seedFile);
            return null;
        }
        // For LRU
        host.touchFile(seedFile);
        return seedFile;
    }
    return null;
}
Also used : FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo) File(java.io.File) Md5Hash(com.fathomdb.hash.Md5Hash)

Example 15 with Md5Hash

use of com.fathomdb.hash.Md5Hash in project platformlayer by platformlayer.

the class JenkinsCasStore method findTag.

@Override
public Md5Hash findTag(String tag) throws OpsException {
    URI uri = client.getBaseUrl();
    // TODO: Match jenkins host??
    String[] tokens = tag.split(":");
    if (tokens.length != 3) {
        return null;
    }
    String jobKey = tokens[0];
    String promotionKey = tokens[1];
    String fileName = tokens[2];
    String treeFilter = "fingerprint[fileName,hash]";
    BuildInfo buildInfo;
    try {
        buildInfo = client.findPromotedBuild(jobKey, promotionKey, treeFilter);
        if (buildInfo == null) {
            return null;
        }
    } catch (JenkinsException e) {
        throw new OpsException("Error communicating with Jenkins", e);
    }
    FingerprintInfo found = null;
    List<FingerprintInfo> fingerprints = buildInfo.getFingerprints();
    for (FingerprintInfo fingerprint : fingerprints) {
        if (fileName.equals(fingerprint.getFileName())) {
            found = fingerprint;
        }
    }
    if (found == null) {
        for (FingerprintInfo fingerprint : fingerprints) {
            String fingerprintFileName = fingerprint.getFileName();
            if (fingerprintFileName.contains(":")) {
                // Strip the maven prefix
                fingerprintFileName = fingerprintFileName.substring(fingerprintFileName.indexOf(':') + 1);
            }
            if (fileName.equals(fingerprintFileName)) {
                found = fingerprint;
            }
        }
    }
    if (found == null) {
        log.warn("Could not find fingerprinted file with name: " + fileName + " in " + buildInfo);
        return null;
    }
    String hash = found.getHash();
    if (hash == null) {
        throw new IllegalStateException();
    }
    // We return the hash in the hope that we've already copied the artifact!
    return new Md5Hash(hash);
}
Also used : OpsException(org.platformlayer.ops.OpsException) BuildInfo(org.platformlayer.ops.cas.jenkins.JenkinsClient.BuildInfo) FingerprintInfo(org.platformlayer.ops.cas.jenkins.JenkinsClient.FingerprintInfo) URI(java.net.URI) Md5Hash(com.fathomdb.hash.Md5Hash)

Aggregations

Md5Hash (com.fathomdb.hash.Md5Hash)15 File (java.io.File)6 OpsException (org.platformlayer.ops.OpsException)4 Command (org.platformlayer.ops.Command)3 DownloadFileByHash (org.platformlayer.ops.filesystem.DownloadFileByHash)3 IOException (java.io.IOException)2 X509Certificate (java.security.cert.X509Certificate)2 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)2 InputStream (java.io.InputStream)1 InetAddress (java.net.InetAddress)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 Path (java.nio.file.Path)1 MessageDigest (java.security.MessageDigest)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 CertificateChainInfo (org.platformlayer.auth.v1.CertificateChainInfo)1 CertificateInfo (org.platformlayer.auth.v1.CertificateInfo)1