|
| 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> with its |
| 42 | + * <a href="https://developer.1password.com/docs/cli/reference/commands/read/">read command</a> to retrieve passwords from |
| 43 | + * 1Password vaults. |
| 44 | + * <p> |
| 45 | + * Config: {@code onepassword:$SECRET_REFERENCE_URI}. |
| 46 | + * The secret reference URI format is outlined at <a href="https://developer.1password.com/docs/cli/secret-reference-syntax">Secret Reference Syntax</a>. |
| 47 | + * @see <a href="https://developer.1password.com/">1Password</a> |
| 48 | + */ |
| 49 | +@Singleton |
| 50 | +@Named(OnePasswordCliMasterSource.NAME) |
| 51 | +public final class OnePasswordCliMasterSource extends PrefixMasterSourceSupport implements MasterSourceMeta { |
| 52 | + public static final String NAME = "onepassword"; |
| 53 | + |
| 54 | + private static final String OP_CLI_EXECUTABLE = "op"; |
| 55 | + |
| 56 | + public OnePasswordCliMasterSource() { |
| 57 | + super(NAME + ":"); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String description() { |
| 62 | + return "1Password CLI (secret reference URI should be edited)"; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Optional<String> configTemplate() { |
| 67 | + return Optional.of(NAME + ":$SECRET_REFERENCE_URI"); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected String doHandle(String transformed) throws SecDispatcherException { |
| 72 | + try { |
| 73 | + return execute1PasswordCli(Arrays.asList("read", transformed, "--no-newline"), 30); |
| 74 | + } catch (Exception e) { |
| 75 | + throw new SecDispatcherException( |
| 76 | + String.format("1Password CLI reported an error reading %s: %s", transformed, e.getMessage()), e); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected SecDispatcher.ValidationResponse doValidateConfiguration(String transformed) { |
| 82 | + HashMap<SecDispatcher.ValidationResponse.Level, List<String>> report = new HashMap<>(); |
| 83 | + boolean isValid = false; |
| 84 | + try { |
| 85 | + execute1PasswordCli(Collections.singleton("--version"), 2); |
| 86 | + try { |
| 87 | + execute1PasswordCli(Arrays.asList("read", transformed, "--no-newline"), 30); |
| 88 | + report.put( |
| 89 | + SecDispatcher.ValidationResponse.Level.INFO, |
| 90 | + List.of("Configured 1Password secret reference exists and is accessible!")); |
| 91 | + isValid = true; |
| 92 | + } catch (IllegalStateException e) { |
| 93 | + report.put( |
| 94 | + SecDispatcher.ValidationResponse.Level.ERROR, |
| 95 | + List.of(String.format( |
| 96 | + "1Password CLI reported an error reading secret item %s: %s", |
| 97 | + transformed, e.getMessage()))); |
| 98 | + } catch (IOException e) { |
| 99 | + report.put( |
| 100 | + SecDispatcher.ValidationResponse.Level.ERROR, |
| 101 | + List.of(String.format("General issue executing 1Password CLI: %s", e.getMessage()))); |
| 102 | + } |
| 103 | + } catch (IllegalStateException e) { |
| 104 | + report.put( |
| 105 | + SecDispatcher.ValidationResponse.Level.ERROR, |
| 106 | + List.of(String.format("1Password CLI reported an error exposing the version: %s", e.getMessage()))); |
| 107 | + } catch (IOException e) { |
| 108 | + report.put( |
| 109 | + SecDispatcher.ValidationResponse.Level.ERROR, |
| 110 | + List.of(String.format("Seems 1Password CLI is not installed: %s", e.getMessage()))); |
| 111 | + } |
| 112 | + return new SecDispatcher.ValidationResponse(getClass().getSimpleName(), isValid, report, List.of()); |
| 113 | + } |
| 114 | + |
| 115 | + public String execute1PasswordCli(Collection<String> arguments, int timeoutSeconds) throws IOException { |
| 116 | + List<String> cmd = new ArrayList<>(); |
| 117 | + cmd.add(OP_CLI_EXECUTABLE); |
| 118 | + cmd.addAll(arguments); |
| 119 | + StringWriter output = new StringWriter(); |
| 120 | + Process process = new ProcessBuilder(cmd.toArray(new String[0])).start(); |
| 121 | + try (BufferedReader reader = process.inputReader()) { |
| 122 | + reader.transferTo(output); |
| 123 | + } |
| 124 | + try { |
| 125 | + process.waitFor(timeoutSeconds, TimeUnit.SECONDS); |
| 126 | + StringWriter error = new StringWriter(); |
| 127 | + try (BufferedReader reader = process.errorReader()) { |
| 128 | + reader.transferTo(error); |
| 129 | + } |
| 130 | + int exitCode = process.exitValue(); |
| 131 | + if (exitCode != 0) { |
| 132 | + throw new IllegalStateException(String.format( |
| 133 | + "1Password CLI process exited with code %d, Error: %s", exitCode, error.toString())); |
| 134 | + } else { |
| 135 | + return output.toString(); |
| 136 | + } |
| 137 | + } catch (InterruptedException e) { |
| 138 | + Thread.currentThread().interrupt(); |
| 139 | + throw new IllegalStateException("1Password CLI process was interrupted", e); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments