1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* Copyright (C) 2024, Thomas Wolf <twolf@apache.org> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.internal.signing.ssh;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.security.PublicKey;
import java.time.Instant;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.lib.GpgConfig;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.signing.ssh.CachingSigningKeyDatabase;
import org.eclipse.jgit.signing.ssh.VerificationException;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.StringUtils;
/**
* A {@link CachingSigningKeyDatabase} using the OpenSSH allowed signers file
* and the OpenSSH key revocation list.
*/
public class OpenSshSigningKeyDatabase implements CachingSigningKeyDatabase {
// Keep caches of allowed signers and KRLs. Cache by canonical path.
private static final int DEFAULT_CACHE_SIZE = 5;
private AtomicInteger cacheSize = new AtomicInteger(DEFAULT_CACHE_SIZE);
private class LRU<K, V> extends LinkedHashMap<K, V> {
private static final long serialVersionUID = 1L;
LRU() {
super(DEFAULT_CACHE_SIZE, 0.75f, true);
}
@Override
protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {
return size() > cacheSize.get();
}
}
private final HashMap<Path, AllowedSigners> allowedSigners = new LRU<>();
private final HashMap<Path, OpenSshKrl> revocations = new LRU<>();
@Override
public boolean isRevoked(Repository repository, GpgConfig config,
PublicKey key) throws IOException {
String fileName = config.getSshRevocationFile();
if (StringUtils.isEmptyOrNull(fileName)) {
return false;
}
File file = getFile(repository, fileName);
OpenSshKrl revocationList;
synchronized (revocations) {
revocationList = revocations.computeIfAbsent(file.toPath(),
OpenSshKrl::new);
}
return revocationList.isRevoked(key);
}
@Override
public String isAllowed(Repository repository, GpgConfig config,
PublicKey key, String namespace, PersonIdent ident)
throws IOException, VerificationException {
String fileName = config.getSshAllowedSignersFile();
if (StringUtils.isEmptyOrNull(fileName)) {
// No file configured. Git would error out.
return null;
}
File file = getFile(repository, fileName);
AllowedSigners allowed;
synchronized (allowedSigners) {
allowed = allowedSigners.computeIfAbsent(file.toPath(),
AllowedSigners::new);
}
Instant gitTime = null;
if (ident != null) {
gitTime = ident.getWhenAsInstant();
}
return allowed.isAllowed(key, namespace, null, gitTime);
}
private File getFile(@NonNull Repository repository, String fileName)
throws IOException {
File file;
if (fileName.startsWith("~/") //$NON-NLS-1$
|| fileName.startsWith('~' + File.separator)) {
file = FS.DETECTED.resolve(FS.DETECTED.userHome(),
fileName.substring(2));
} else {
file = new File(fileName);
if (!file.isAbsolute()) {
file = new File(repository.getWorkTree(), fileName);
}
}
return file.getCanonicalFile();
}
@Override
public int getCacheSize() {
return cacheSize.get();
}
@Override
public void setCacheSize(int size) {
if (size > 0) {
cacheSize.set(size);
pruneCache(size);
}
}
private void pruneCache(int size) {
prune(allowedSigners, size);
prune(revocations, size);
}
private void prune(HashMap<?, ?> map, int size) {
synchronized (map) {
if (map.size() <= size) {
return;
}
Iterator<?> iter = map.entrySet().iterator();
int i = 0;
while (iter.hasNext() && i < size) {
iter.next();
i++;
}
while (iter.hasNext()) {
iter.next();
iter.remove();
}
}
}
@Override
public void clearCache() {
synchronized (allowedSigners) {
allowedSigners.clear();
}
synchronized (revocations) {
revocations.clear();
}
}
}
|