|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package jdk.jpackage.internal; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Objects; |
| 33 | +import java.util.stream.Stream; |
| 34 | +import jdk.jpackage.internal.util.PathUtils; |
| 35 | + |
| 36 | +record Keychain(String name) { |
| 37 | + Keychain { |
| 38 | + Objects.requireNonNull(name); |
| 39 | + } |
| 40 | + |
| 41 | + Path path() { |
| 42 | + final var path = Path.of(name); |
| 43 | + if (path.isAbsolute()) { |
| 44 | + return path; |
| 45 | + } else { |
| 46 | + final var dir = Path.of(System.getProperty("user.home")).resolve("Library/Keychains"); |
| 47 | + final var files = filenames(name).map(dir::resolve).toList(); |
| 48 | + return files.stream().filter(Files::exists).findFirst().orElseGet(() -> { |
| 49 | + // Can't find keychain file in "$HOME/Library/Keychains" folder. |
| 50 | + // Detect keychain file name from the name of the login keychain file. |
| 51 | + return files.stream().filter(f -> { |
| 52 | + final var loginKeycahinFile = f.getParent().resolve("login.keychain" + f.getFileName().toString().substring(name.length())); |
| 53 | + return Files.exists(loginKeycahinFile); |
| 54 | + }).findFirst().orElseGet(() -> { |
| 55 | + // login keychain file doesn't exist, fallback to "$HOME/Library/Keychains/<name>-db" keychain file. |
| 56 | + return files.getFirst(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + String asCliArg() { |
| 63 | + final var path = Path.of(name); |
| 64 | + if (path.isAbsolute()) { |
| 65 | + return PathUtils.normalizedAbsolutePathString(path); |
| 66 | + } else { |
| 67 | + return name; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + static List<Keychain> listKeychains() { |
| 72 | + // Get the current keychain list |
| 73 | + final List<String> cmdOutput; |
| 74 | + try { |
| 75 | + cmdOutput = Executor.of("/usr/bin/security", "list-keychains").saveOutput(true).executeExpectSuccess().getOutput(); |
| 76 | + } catch (IOException ex) { |
| 77 | + throw I18N.buildException().message("message.keychain.error").cause(ex).create(KeychainException::new); |
| 78 | + } |
| 79 | + |
| 80 | + // Typical output of /usr/bin/security command is: |
| 81 | + // "/Users/foo/Library/Keychains/login.keychain-db" |
| 82 | + // "/Library/Keychains/System.keychain" |
| 83 | + return cmdOutput.stream().map(String::trim).map(str -> { |
| 84 | + // Strip enclosing double quotes |
| 85 | + return str.substring(1, str.length() - 1); |
| 86 | + }).map(Keychain::new).toList(); |
| 87 | + } |
| 88 | + |
| 89 | + final static class KeychainException extends RuntimeException { |
| 90 | + |
| 91 | + KeychainException(String msg) { |
| 92 | + super(msg); |
| 93 | + } |
| 94 | + |
| 95 | + KeychainException(String msg, Throwable cause) { |
| 96 | + super(msg, cause); |
| 97 | + } |
| 98 | + |
| 99 | + private static final long serialVersionUID = 1L; |
| 100 | + } |
| 101 | + |
| 102 | + private static Stream<String> filenames(String name) { |
| 103 | + return Stream.of(name + "-db", name); |
| 104 | + } |
| 105 | +} |
0 commit comments