|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 2026, 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.Closeable; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Objects; |
| 33 | +import java.util.Optional; |
| 34 | +import java.util.function.Consumer; |
| 35 | +import java.util.stream.Collectors; |
| 36 | +import java.util.stream.Stream; |
| 37 | +import jdk.internal.util.OSVersion; |
| 38 | + |
| 39 | +final class ActiveKeychainList implements Closeable { |
| 40 | + |
| 41 | + static Optional<ActiveKeychainList> createForPlatform(List<Keychain> keychains) throws IOException { |
| 42 | + if (!keychains.isEmpty() && Globals.instance().findBooleanProperty(ActiveKeychainList.class).orElseGet(ActiveKeychainList::isRequired)) { |
| 43 | + return Optional.of(new ActiveKeychainList(keychains)); |
| 44 | + } else { |
| 45 | + return Optional.empty(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static Optional<ActiveKeychainList> createForPlatform(Keychain... keychains) throws IOException { |
| 50 | + return createForPlatform(List.of(keychains)); |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings("try") |
| 54 | + static void withKeychains(Consumer<List<Keychain>> keychainConsumer, List<Keychain> keychains) throws IOException { |
| 55 | + var keychainList = createForPlatform(keychains); |
| 56 | + if (keychainList.isEmpty()) { |
| 57 | + keychainConsumer.accept(keychains); |
| 58 | + } else { |
| 59 | + try (var kl = keychainList.get()) { |
| 60 | + keychainConsumer.accept(keychains); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + static void withKeychain(Consumer<Keychain> keychainConsumer, Keychain keychain) throws IOException { |
| 66 | + |
| 67 | + Objects.requireNonNull(keychainConsumer); |
| 68 | + withKeychains(keychains -> { |
| 69 | + keychainConsumer.accept(keychains.getFirst()); |
| 70 | + }, List.of(keychain)); |
| 71 | + } |
| 72 | + |
| 73 | + ActiveKeychainList(List<Keychain> requestedKeychains, List<Keychain> currentKeychains, boolean force) throws IOException { |
| 74 | + this.requestedKeychains = List.copyOf(requestedKeychains); |
| 75 | + this.oldKeychains = List.copyOf(currentKeychains); |
| 76 | + |
| 77 | + final List<String> cmdline = new ArrayList<>(LIST_KEYCHAINS_CMD_PREFIX); |
| 78 | + addKeychains(cmdline, oldKeychains); |
| 79 | + |
| 80 | + if (force) { |
| 81 | + this.currentKeychains = requestedKeychains; |
| 82 | + restoreKeychainsCmd = List.copyOf(cmdline); |
| 83 | + cmdline.subList(LIST_KEYCHAINS_CMD_PREFIX.size(), cmdline.size()).clear(); |
| 84 | + addKeychains(cmdline, requestedKeychains); |
| 85 | + } else { |
| 86 | + final var currentKeychainPaths = oldKeychains.stream().map(Keychain::path).toList(); |
| 87 | + |
| 88 | + final var missingKeychains = requestedKeychains.stream().filter(k -> { |
| 89 | + return !currentKeychainPaths.contains(k.path()); |
| 90 | + }).toList(); |
| 91 | + |
| 92 | + if (missingKeychains.isEmpty()) { |
| 93 | + this.currentKeychains = oldKeychains; |
| 94 | + restoreKeychainsCmd = List.of(); |
| 95 | + } else { |
| 96 | + this.currentKeychains = Stream.of(oldKeychains, missingKeychains) |
| 97 | + .flatMap(List::stream).collect(Collectors.toUnmodifiableList()); |
| 98 | + restoreKeychainsCmd = List.copyOf(cmdline); |
| 99 | + addKeychains(cmdline, missingKeychains); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + Executor.of(cmdline).executeExpectSuccess(); |
| 104 | + } |
| 105 | + |
| 106 | + ActiveKeychainList(List<Keychain> keychains) throws IOException { |
| 107 | + this(keychains, Keychain.listKeychains(), false); |
| 108 | + } |
| 109 | + |
| 110 | + List<Keychain> requestedKeychains() { |
| 111 | + return requestedKeychains; |
| 112 | + } |
| 113 | + |
| 114 | + List<Keychain> currentKeychains() { |
| 115 | + return currentKeychains; |
| 116 | + } |
| 117 | + |
| 118 | + List<Keychain> restoreKeychains() { |
| 119 | + return oldKeychains; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void close() throws IOException { |
| 124 | + if (!restoreKeychainsCmd.isEmpty()) { |
| 125 | + Executor.of(restoreKeychainsCmd).executeExpectSuccess(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static void addKeychains(List<String> cmdline, List<Keychain> keychains) { |
| 130 | + cmdline.addAll(keychains.stream().map(Keychain::asCliArg).toList()); |
| 131 | + } |
| 132 | + |
| 133 | + private static boolean isRequired() { |
| 134 | + // Required for OS X 10.12+ |
| 135 | + return 0 <= OSVersion.current().compareTo(new OSVersion(10, 12)); |
| 136 | + } |
| 137 | + |
| 138 | + private final List<Keychain> requestedKeychains; |
| 139 | + private final List<Keychain> currentKeychains; |
| 140 | + private final List<Keychain> oldKeychains; |
| 141 | + private final List<String> restoreKeychainsCmd; |
| 142 | + |
| 143 | + private final static List<String> LIST_KEYCHAINS_CMD_PREFIX = List.of("/usr/bin/security", "list-keychains", "-s"); |
| 144 | +} |
0 commit comments