View Javadoc
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  
45  import static java.text.MessageFormat.format;
46  
47  import java.io.IOException;
48  import java.nio.file.Files;
49  import java.nio.file.Path;
50  import java.security.GeneralSecurityException;
51  import java.security.KeyPair;
52  import java.util.ArrayList;
53  import java.util.Collection;
54  import java.util.Collections;
55  import java.util.Iterator;
56  import java.util.List;
57  import java.util.NoSuchElementException;
58  import java.util.concurrent.CancellationException;
59  
60  import org.eclipse.jgit.transport.sshd.KeyCache;
61  
62  /**
63   * A {@link EncryptedFileKeyPairProvider} that uses an external
64   * {@link KeyCache}.
65   */
66  public class CachingKeyPairProvider extends EncryptedFileKeyPairProvider
67  		implements Iterable<KeyPair> {
68  
69  	private final KeyCache cache;
70  
71  	/**
72  	 * Creates a new {@link CachingKeyPairProvider} using the given
73  	 * {@link KeyCache}. If the cache is {@code null}, this is a simple
74  	 * {@link EncryptedFileKeyPairProvider}.
75  	 *
76  	 * @param paths
77  	 *            to load keys from
78  	 * @param cache
79  	 *            to use, may be {@code null} if no external caching is desired
80  	 */
81  	public CachingKeyPairProvider(List<Path> paths, KeyCache cache) {
82  		super(paths);
83  		this.cache = cache;
84  	}
85  
86  	@Override
87  	public Iterator<KeyPair> iterator() {
88  		Collection<? extends Path> resources = getPaths();
89  		if (resources.isEmpty()) {
90  			return Collections.emptyListIterator();
91  		}
92  		return new CancellingKeyPairIterator(resources);
93  	}
94  
95  	@Override
96  	public Iterable<KeyPair> loadKeys() {
97  		return this;
98  	}
99  
100 	@Override
101 	protected KeyPair doLoadKey(Path resource)
102 			throws IOException, GeneralSecurityException {
103 		if (!Files.exists(resource)) {
104 			log.warn(format(SshdText.get().identityFileNotFound, resource));
105 			return null;
106 		}
107 		// By calling doLoadKey(String, Path, FilePasswordProvider) instead of
108 		// super.doLoadKey(Path) we can bypass the key caching in
109 		// AbstractResourceKeyPairProvider, over which we have no real control.
110 		String resourceId = resource.toString();
111 		if (cache == null) {
112 			return doLoadKey(resourceId, resource, getPasswordFinder());
113 		}
114 		Throwable t[] = { null };
115 		KeyPair key = cache.get(resource, p -> {
116 			try {
117 				return doLoadKey(resourceId, p, getPasswordFinder());
118 			} catch (IOException | GeneralSecurityException e) {
119 				t[0] = e;
120 				return null;
121 			}
122 		});
123 		if (t[0] != null) {
124 			if (t[0] instanceof CancellationException) {
125 				throw (CancellationException) t[0];
126 			}
127 			throw new IOException(
128 					format(SshdText.get().keyLoadFailed, resource), t[0]);
129 		}
130 		return key;
131 	}
132 
133 	private class CancellingKeyPairIterator implements Iterator<KeyPair> {
134 
135 		private final Iterator<Path> paths;
136 
137 		private KeyPair nextItem;
138 
139 		private boolean nextSet;
140 
141 		public CancellingKeyPairIterator(Collection<? extends Path> resources) {
142 			List<Path> copy = new ArrayList<>(resources.size());
143 			copy.addAll(resources);
144 			paths = copy.iterator();
145 		}
146 
147 		@Override
148 		public boolean hasNext() {
149 			if (nextSet) {
150 				return nextItem != null;
151 			}
152 			nextSet = true;
153 			while (nextItem == null && paths.hasNext()) {
154 				try {
155 					nextItem = doLoadKey(paths.next());
156 				} catch (CancellationException cancelled) {
157 					throw cancelled;
158 				} catch (Exception other) {
159 					log.warn(other.toString());
160 				}
161 			}
162 			return nextItem != null;
163 		}
164 
165 		@Override
166 		public KeyPair next() {
167 			if (!nextSet && !hasNext()) {
168 				throw new NoSuchElementException();
169 			}
170 			KeyPair result = nextItem;
171 			nextItem = null;
172 			nextSet = false;
173 			if (result == null) {
174 				throw new NoSuchElementException();
175 			}
176 			return result;
177 		}
178 
179 	}
180 }