Skip to content

Commit

Permalink
#1714 .gnupg everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 3, 2023
1 parent 814bb4d commit e9eaf14
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
19 changes: 15 additions & 4 deletions src/main/java/com/rultor/agents/daemons/StartsDaemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
@SuppressWarnings("PMD.ExcessiveImports")
public final class StartsDaemon implements Agent {

/**
* Gpg home dir.
*/
public static final String GPG_HOME = ".gnupg";

/**
* Paths to match.
*/
Expand All @@ -82,7 +87,6 @@ public final class StartsDaemon implements Agent {
"/talk/daemon[script and dir and not(started) and not(ended)]",
"/talk/daemon[dir != '']"
);

/**
* Profile to get assets from.
*/
Expand Down Expand Up @@ -247,15 +251,22 @@ private void gpg(final Shell shell, final String dir) throws IOException {
String.join(
" && ",
String.format("cd %s ", Ssh.escape(dir)),
"mkdir -p .gpg",
String.format("cat > \".gpg/%s\"", name)
String.format("mkdir -p %s", StartsDaemon.GPG_HOME),
String.format(
"cat > \"%s/%s\"",
StartsDaemon.GPG_HOME, name
)
),
this.ring(name),
Logger.stream(Level.INFO, true),
Logger.stream(Level.WARNING, true)
);
}
Logger.info(this, "GPG keys uploaded to %s", dir);
Logger.info(
this,
"GPG keys uploaded to %s/%s",
dir, StartsDaemon.GPG_HOME
);
}
}

Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/rultor/agents/req/Decrypt.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.jcabi.manifests.Manifests;
import com.jcabi.ssh.Ssh;
import com.jcabi.xml.XML;
import com.rultor.agents.daemons.StartsDaemon;
import com.rultor.spi.Profile;
import java.io.IOException;
import java.util.Collection;
Expand Down Expand Up @@ -117,12 +118,20 @@ public Iterable<String> commands() throws IOException {
String.join(
Decrypt.SPACE,
"gpg --import",
"\"$(pwd)/.gnupg/secring.gpg\""
String.format(
"\"$(pwd)/%s/secring.gpg\"",
StartsDaemon.GPG_HOME
)
)
);
commands.add("gpg --version");
commands.add("gpg --list-keys");
commands.add("ls -al \"$(pwd)/.gnupg\"");
commands.add(
String.format(
"ls -al \"$(pwd)/%s\"",
StartsDaemon.GPG_HOME
)
);
}
for (final XML asset : assets) {
final String key = asset.xpath("@key").get(0);
Expand Down Expand Up @@ -153,9 +162,9 @@ public Iterable<String> commands() throws IOException {
Ssh.escape(key)
)
);
commands.add(String.format("rm -rf %s", Ssh.escape(enc)));
commands.add(String.format("rm -rf %s ", Ssh.escape(enc)));
}
commands.add("rm -rf .gnupg");
commands.add(String.format("rm -rf %s", StartsDaemon.GPG_HOME));
if (!assets.isEmpty()) {
commands.add(
String.format(
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/com/rultor/agents/req/DecryptTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.jcabi.log.VerboseProcess;
import com.jcabi.xml.XMLDocument;
import com.rultor.agents.daemons.StartsDaemon;
import com.rultor.spi.Profile;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -115,7 +116,10 @@ public void decryptsAssets() throws Exception {
);
Assume.assumeThat(gpg, Matchers.not(Matchers.startsWith("${")));
FileUtils.writeByteArrayToFile(
new File(dir, String.format(".gnupg/%s.gpg", key)),
new File(
dir,
String.format("%s/%s.gpg", StartsDaemon.GPG_HOME, key)
),
Base64.decodeBase64(gpg)
);
}
Expand Down

0 comments on commit e9eaf14

Please sign in to comment.