You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CachingKeyPairProvider.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.transport.sshd;
  44. import static java.text.MessageFormat.format;
  45. import java.io.IOException;
  46. import java.nio.file.Files;
  47. import java.nio.file.Path;
  48. import java.security.GeneralSecurityException;
  49. import java.security.KeyPair;
  50. import java.util.ArrayList;
  51. import java.util.Collection;
  52. import java.util.Collections;
  53. import java.util.Iterator;
  54. import java.util.List;
  55. import java.util.NoSuchElementException;
  56. import java.util.concurrent.CancellationException;
  57. import org.eclipse.jgit.transport.sshd.KeyCache;
  58. /**
  59. * A {@link EncryptedFileKeyPairProvider} that uses an external
  60. * {@link KeyCache}.
  61. */
  62. public class CachingKeyPairProvider extends EncryptedFileKeyPairProvider {
  63. private final KeyCache cache;
  64. /**
  65. * Creates a new {@link CachingKeyPairProvider} using the given
  66. * {@link KeyCache}. If the cache is {@code null}, this is a simple
  67. * {@link EncryptedFileKeyPairProvider}.
  68. *
  69. * @param paths
  70. * to load keys from
  71. * @param cache
  72. * to use, may be {@code null} if no external caching is desired
  73. */
  74. public CachingKeyPairProvider(List<Path> paths, KeyCache cache) {
  75. super(paths);
  76. this.cache = cache;
  77. }
  78. @Override
  79. protected Iterable<KeyPair> loadKeys(Collection<? extends Path> resources) {
  80. if (resources.isEmpty()) {
  81. return Collections.emptyList();
  82. }
  83. return () -> new CancellingKeyPairIterator(resources);
  84. }
  85. @Override
  86. protected KeyPair doLoadKey(Path resource)
  87. throws IOException, GeneralSecurityException {
  88. if (!Files.exists(resource)) {
  89. log.warn(format(SshdText.get().identityFileNotFound, resource));
  90. return null;
  91. }
  92. // By calling doLoadKey(String, Path, FilePasswordProvider) instead of
  93. // super.doLoadKey(Path) we can bypass the key caching in
  94. // AbstractResourceKeyPairProvider, over which we have no real control.
  95. String resourceId = resource.toString();
  96. if (cache == null) {
  97. return doLoadKey(resourceId, resource, getPasswordFinder());
  98. }
  99. Throwable t[] = { null };
  100. KeyPair key = cache.get(resource, p -> {
  101. try {
  102. return doLoadKey(resourceId, p, getPasswordFinder());
  103. } catch (IOException | GeneralSecurityException e) {
  104. t[0] = e;
  105. return null;
  106. }
  107. });
  108. if (t[0] != null) {
  109. if (t[0] instanceof CancellationException) {
  110. throw (CancellationException) t[0];
  111. }
  112. throw new IOException(
  113. format(SshdText.get().keyLoadFailed, resource), t[0]);
  114. }
  115. return key;
  116. }
  117. private class CancellingKeyPairIterator implements Iterator<KeyPair> {
  118. private final Iterator<Path> paths;
  119. private KeyPair nextItem;
  120. private boolean nextSet;
  121. public CancellingKeyPairIterator(Collection<? extends Path> resources) {
  122. List<Path> copy = new ArrayList<>(resources.size());
  123. copy.addAll(resources);
  124. paths = copy.iterator();
  125. }
  126. @Override
  127. public boolean hasNext() {
  128. if (nextSet) {
  129. return nextItem != null;
  130. }
  131. nextSet = true;
  132. while (nextItem == null && paths.hasNext()) {
  133. try {
  134. nextItem = doLoadKey(paths.next());
  135. } catch (CancellationException cancelled) {
  136. throw cancelled;
  137. } catch (Exception other) {
  138. log.warn(other.toString());
  139. }
  140. }
  141. return nextItem != null;
  142. }
  143. @Override
  144. public KeyPair next() {
  145. if (!nextSet && !hasNext()) {
  146. throw new NoSuchElementException();
  147. }
  148. KeyPair result = nextItem;
  149. nextItem = null;
  150. nextSet = false;
  151. if (result == null) {
  152. throw new NoSuchElementException();
  153. }
  154. return result;
  155. }
  156. }
  157. }