• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jreleaser / jreleaser / #547

30 Oct 2025 07:48PM UTC coverage: 48.255% (-0.001%) from 48.256%
#547

push

github

aalmiray
deps: Update zstd-jni to 1.5.7-6

26017 of 53916 relevant lines covered (48.25%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/sdks/jreleaser-signing-java-sdk/src/main/java/org/jreleaser/sdk/signing/GpgCommandSigner.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 *
4
 * Copyright 2020-2025 The JReleaser authors.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     https://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
package org.jreleaser.sdk.signing;
19

20
import org.jreleaser.bundle.RB;
21
import org.jreleaser.logging.JReleaserLogger;
22
import org.jreleaser.sdk.command.Command;
23
import org.jreleaser.sdk.command.CommandException;
24
import org.jreleaser.sdk.command.CommandExecutor;
25

26
import java.io.ByteArrayInputStream;
27
import java.io.IOException;
28
import java.nio.file.Files;
29
import java.nio.file.Path;
30
import java.util.ArrayList;
31
import java.util.List;
32
import java.util.UUID;
33

34
import static java.nio.charset.StandardCharsets.UTF_8;
35
import static java.nio.file.StandardOpenOption.WRITE;
36
import static org.jreleaser.util.StringUtils.isNotBlank;
37

38
/**
39
 * @author Andres Almiray
40
 * @since 0.8.0
41
 */
42
public final class GpgCommandSigner {
43
    private final JReleaserLogger logger;
44
    private final List<String> args = new ArrayList<>();
×
45

46
    private String executable;
47
    private String passphrase;
48
    private String keyName;
49
    private String homeDir;
50
    private String publicKeyring;
51
    private boolean defaultKeyring;
52

53
    public GpgCommandSigner(JReleaserLogger logger) {
×
54
        this.logger = logger;
×
55
    }
×
56

57
    public void setExecutable(String executable) {
58
        this.executable = executable;
×
59
    }
×
60

61
    public void setPassphrase(String passphrase) {
62
        this.passphrase = passphrase;
×
63
    }
×
64

65
    public void setKeyName(String keyName) {
66
        this.keyName = keyName;
×
67
    }
×
68

69
    public void setHomeDir(String homeDir) {
70
        this.homeDir = homeDir;
×
71
    }
×
72

73
    public void setPublicKeyring(String publicKeyring) {
74
        this.publicKeyring = publicKeyring;
×
75
    }
×
76

77
    public void setDefaultKeyring(boolean defaultKeyring) {
78
        this.defaultKeyring = defaultKeyring;
×
79
    }
×
80

81
    public void setArgs(List<String> args) {
82
        this.args.clear();
×
83
        this.args.addAll(args);
×
84
    }
×
85

86
    public void sign(Path input, Path output) throws CommandException {
87
        Command cmd = createSignCommand();
×
88

89
        cmd.arg("--output")
×
90
            .arg(output.toAbsolutePath().toString())
×
91
            .arg(input.toAbsolutePath().toString());
×
92

93
        final ByteArrayInputStream stdin = isNotBlank(passphrase)
×
94
            ? new ByteArrayInputStream(passphrase.getBytes(UTF_8))
×
95
            : new ByteArrayInputStream(new byte[]{});
×
96
        Command.Result result = new CommandExecutor(logger)
×
97
            .executeCommand(cmd, stdin);
×
98
        if (result.getExitValue() != 0) {
×
99
            throw new CommandException(RB.$("ERROR_command_execution_exit_value", result.getExitValue()));
×
100
        }
101
    }
×
102

103
    public boolean verify(Path signature, Path target) throws CommandException {
104
        Command cmd = createVerifyCommand()
×
105
            .arg(signature.toAbsolutePath().toString())
×
106
            .arg(target.toAbsolutePath().toString());
×
107
        return new CommandExecutor(logger, CommandExecutor.Output.QUIET)
×
108
            .executeCommand(cmd).getExitValue() == 0;
×
109
    }
110

111
    public byte[] sign(byte[] in) throws CommandException {
112
        try {
113
            Path input = Files.createTempFile("jreleaser", "sign-input");
×
114
            Files.write(input, in, WRITE);
×
115
            Path output = Files.createTempDirectory("jreleaser-" + UUID.randomUUID())
×
116
                .resolve(UUID.randomUUID().toString());
×
117

118
            sign(input, output);
×
119

120
            return Files.readAllBytes(output);
×
121
        } catch (IOException e) {
×
122
            throw new CommandException(RB.$("ERROR_unexpected_error"), e);
×
123
        }
124
    }
125

126
    private Command createSignCommand() {
127
        Command cmd = new Command(executable)
×
128
            .args(args);
×
129

130
        if (isNotBlank(homeDir)) {
×
131
            cmd.arg("--homedir")
×
132
                .arg(homeDir);
×
133
        }
134
        if (isNotBlank(keyName)) {
×
135
            cmd.arg("--local-user")
×
136
                .arg(keyName);
×
137
        }
138

139
        cmd.arg("--armor")
×
140
            .arg("--detach-sign")
×
141
            .arg("--batch")
×
142
            .arg("--no-tty");
×
143

144
        if (isNotBlank(passphrase)) {
×
145
            cmd.arg("--pinentry-mode")
×
146
                .arg("loopback")
×
147
                .arg("--passphrase-fd")
×
148
                .arg("0");
×
149
        }
150

151
        if (!defaultKeyring) {
×
152
            cmd.arg("--no-default-keyring");
×
153
        }
154

155
        if (isNotBlank(publicKeyring)) {
×
156
            cmd.arg("--keyring")
×
157
                .arg(publicKeyring);
×
158
        }
159

160
        return cmd;
×
161
    }
162

163
    private Command createVerifyCommand() {
164
        Command cmd = new Command(executable)
×
165
            .args(args);
×
166

167
        if (isNotBlank(homeDir)) {
×
168
            cmd.arg("--homedir")
×
169
                .arg(homeDir);
×
170
        }
171
        if (isNotBlank(keyName)) {
×
172
            cmd.arg("--local-user")
×
173
                .arg(keyName);
×
174
        }
175

176
        if (!defaultKeyring) {
×
177
            cmd.arg("--no-default-keyring");
×
178
        }
179

180
        if (isNotBlank(publicKeyring)) {
×
181
            cmd.arg("--keyring")
×
182
                .arg(publicKeyring);
×
183
        }
184

185
        cmd.arg("--verify");
×
186

187
        return cmd;
×
188
    }
189
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc