Skip to content

Commit 798ca46

Browse files
committed
Add 1Password CLI integration
This closes #86
1 parent 87a2ff1 commit 798ca46

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.codehaus.plexus.components.secdispatcher.internal.sources;
20+
21+
import javax.inject.Named;
22+
import javax.inject.Singleton;
23+
24+
import java.io.BufferedReader;
25+
import java.io.IOException;
26+
import java.io.StringWriter;
27+
import java.util.ArrayList;
28+
import java.util.Arrays;
29+
import java.util.Collection;
30+
import java.util.Collections;
31+
import java.util.HashMap;
32+
import java.util.List;
33+
import java.util.Optional;
34+
import java.util.concurrent.TimeUnit;
35+
36+
import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
37+
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
38+
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
39+
40+
/**
41+
* Password source that uses <a href="https://developer.1password.com/docs/cli/get-started">1Password CLI</a>.
42+
* <p>
43+
* Config: {@code onepassword:$SECRET_REFERENCE_URI}.
44+
* The secret reference URI format is outlined at <a href="https://developer.1password.com/docs/cli/secret-reference-syntax">Secret Reference Syntax</a>
45+
*/
46+
@Singleton
47+
@Named(OnePasswordCliMasterSource.NAME)
48+
public final class OnePasswordCliMasterSource extends PrefixMasterSourceSupport implements MasterSourceMeta {
49+
public static final String NAME = "onepassword";
50+
51+
private static final String OP_CLI_EXECUTABLE = "op";
52+
53+
public OnePasswordCliMasterSource() {
54+
super(NAME + ":");
55+
}
56+
57+
@Override
58+
public String description() {
59+
return "1Password CLI (secret reference URI should be edited)";
60+
}
61+
62+
@Override
63+
public Optional<String> configTemplate() {
64+
return Optional.of(NAME + ":$SECRET_REFERENCE_URI");
65+
}
66+
67+
@Override
68+
protected String doHandle(String transformed) throws SecDispatcherException {
69+
try {
70+
return execute1PasswordCli(Arrays.asList("read", transformed, "--no-newline"), 30);
71+
} catch (Exception e) {
72+
throw new SecDispatcherException(
73+
String.format("1Password CLI reported an error reading %s: %s", transformed, e.getMessage()), e);
74+
}
75+
}
76+
77+
@Override
78+
protected SecDispatcher.ValidationResponse doValidateConfiguration(String transformed) {
79+
HashMap<SecDispatcher.ValidationResponse.Level, List<String>> report = new HashMap<>();
80+
boolean isValid = false;
81+
try {
82+
execute1PasswordCli(Collections.singleton("--version"), 2);
83+
try {
84+
execute1PasswordCli(Arrays.asList("read", transformed, "--no-newline"), 30);
85+
report.put(
86+
SecDispatcher.ValidationResponse.Level.INFO,
87+
List.of("Configured 1Password secret reference exists and is accessible!"));
88+
isValid = true;
89+
} catch (IllegalStateException e) {
90+
report.put(
91+
SecDispatcher.ValidationResponse.Level.ERROR,
92+
List.of(String.format(
93+
"1Password CLI reported an error reading secret item %s: %s",
94+
transformed, e.getMessage())));
95+
} catch (IOException e) {
96+
report.put(
97+
SecDispatcher.ValidationResponse.Level.ERROR,
98+
List.of(String.format("General issue executing 1Password CLI: %s", e.getMessage())));
99+
}
100+
} catch (IllegalStateException e) {
101+
report.put(
102+
SecDispatcher.ValidationResponse.Level.ERROR,
103+
List.of(String.format("1Password CLI reported an error exposing the version: %s", e.getMessage())));
104+
} catch (IOException e) {
105+
report.put(
106+
SecDispatcher.ValidationResponse.Level.ERROR,
107+
List.of(String.format("Seems 1Password CLI is not installed: %s", e.getMessage())));
108+
}
109+
return new SecDispatcher.ValidationResponse(getClass().getSimpleName(), isValid, report, List.of());
110+
}
111+
112+
public String execute1PasswordCli(Collection<String> arguments, int timeoutSeconds) throws IOException {
113+
List<String> cmd = new ArrayList<>();
114+
cmd.add(OP_CLI_EXECUTABLE);
115+
cmd.addAll(arguments);
116+
StringWriter output = new StringWriter();
117+
Process process = new ProcessBuilder(cmd.toArray(new String[0])).start();
118+
try (BufferedReader reader = process.inputReader()) {
119+
reader.transferTo(output);
120+
}
121+
try {
122+
process.waitFor(timeoutSeconds, TimeUnit.SECONDS);
123+
StringWriter error = new StringWriter();
124+
try (BufferedReader reader = process.errorReader()) {
125+
reader.transferTo(error);
126+
}
127+
int exitCode = process.exitValue();
128+
if (exitCode != 0) {
129+
throw new IllegalStateException(String.format(
130+
"1Password CLI process exited with code %d, Error: %s", exitCode, error.toString()));
131+
} else {
132+
return output.toString();
133+
}
134+
} catch (InterruptedException e) {
135+
Thread.currentThread().interrupt();
136+
throw new IllegalStateException("1Password CLI process was interrupted", e);
137+
}
138+
}
139+
}

src/test/java/org/codehaus/plexus/components/secdispatcher/internal/sources/SourcesTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,13 @@ void pinEntry() {
4949
// ypu may adjust path, this is Fedora40 WS + gnome
5050
assertEquals("masterPw", source.handle("pinentry-prompt:/usr/bin/pinentry-gnome3"));
5151
}
52+
53+
@Disabled("enable and add 1Passwort item with password 'masterPw'")
54+
@Test
55+
void onePassword() {
56+
OnePasswordCliMasterSource source = new OnePasswordCliMasterSource();
57+
// assume you have 1Password CLI installed and vault "Employee" contains item "Maven Master" with field
58+
// "password"
59+
assertEquals("masterPw", source.handle("onepassword:op://Employee/Maven Master/password"));
60+
}
5261
}

0 commit comments

Comments
 (0)