Skip to content

Commit 40040b6

Browse files
committed
The public key manager can disable writing keys, which hides commands
Some public key mangers may be read-only, i.e. not allow to add or delete keys, or to change the key comment or assigned permissions. In such a case the respective commands should not be available on the SSH shell and the SSH Keys panel should also not offer the possibility. The `IPublicKeyManager` gets three new methods, modelled after the `AuthenticationManager`: `supportsWritingKeys`, `supportsCommentChanges` and `supportsPermissionChanges`. They return true if a key manager allows for keys to be written or updated. For example the existing `FileKeyManager` will return true for all three since it allows to store and update keys in a file. The new `LdapKeyManager` returns false since it only accesses LDAP and can not add or update any keys in the directory. A future key manager might get keys from an LDAP directory but still keep comments and permissions for it in a local copy. If writing of keys is not supported: * the welcome shell does not suggest adding a key, * the `SshKeysDispatcher` does not offer the "add", "remove", "comment" and "permission" commands, and * the SSH keys panel hides the "delete" button in the key list, and the "Add Key" form. The hiding of the "Add key" form is not perfect since the surrounding div is still shown, but I don't know how to hide it and it didn't look too bad, either.
1 parent a3f9b4f commit 40040b6

File tree

6 files changed

+63
-12
lines changed

6 files changed

+63
-12
lines changed

src/main/java/com/gitblit/transport/ssh/IPublicKeyManager.java

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.slf4j.LoggerFactory;
2626

2727
import com.gitblit.manager.IManager;
28+
import com.gitblit.models.UserModel;
2829
import com.google.common.cache.CacheBuilder;
2930
import com.google.common.cache.CacheLoader;
3031
import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
@@ -99,4 +100,16 @@ public final void renameUser(String oldName, String newName) {
99100
public abstract boolean removeKey(String username, SshKey key);
100101

101102
public abstract boolean removeAllKeys(String username);
103+
104+
public boolean supportsWritingKeys(UserModel user) {
105+
return (user != null);
106+
}
107+
108+
public boolean supportsCommentChanges(UserModel user) {
109+
return (user != null);
110+
}
111+
112+
public boolean supportsPermissionChanges(UserModel user) {
113+
return (user != null);
114+
}
102115
}

src/main/java/com/gitblit/transport/ssh/LdapKeyManager.java

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.gitblit.Keys;
3535
import com.gitblit.Constants.AccessPermission;
3636
import com.gitblit.ldap.LdapConnection;
37+
import com.gitblit.models.UserModel;
3738
import com.gitblit.utils.StringUtils;
3839
import com.google.common.base.Joiner;
3940
import com.google.inject.Inject;
@@ -219,6 +220,18 @@ public boolean removeAllKeys(String username) {
219220
}
220221

221222

223+
public boolean supportsWritingKeys(UserModel user) {
224+
return false;
225+
}
226+
227+
public boolean supportsCommentChanges(UserModel user) {
228+
return false;
229+
}
230+
231+
public boolean supportsPermissionChanges(UserModel user) {
232+
return false;
233+
}
234+
222235

223236
private void setKeyPermissions(SshKey key, GbAuthorizedKeyEntry keyEntry) {
224237
List<String> env = keyEntry.getLoginOptionValues("environment");

src/main/java/com/gitblit/transport/ssh/SshDaemon.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public SshDaemon(IGitblit gitblit, WorkQueue workQueue) {
134134
sshd.setFileSystemFactory(new DisabledFilesystemFactory());
135135
sshd.setTcpipForwardingFilter(new NonForwardingFilter());
136136
sshd.setCommandFactory(new SshCommandFactory(gitblit, workQueue));
137-
sshd.setShellFactory(new WelcomeShell(settings));
137+
sshd.setShellFactory(new WelcomeShell(gitblit));
138138

139139
// Set the server id. This can be queried with:
140140
// ssh-keyscan -t rsa,dsa -p 29418 localhost

src/main/java/com/gitblit/transport/ssh/WelcomeShell.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import com.gitblit.IStoredSettings;
3636
import com.gitblit.Keys;
37+
import com.gitblit.manager.IGitblit;
3738
import com.gitblit.models.UserModel;
3839
import com.gitblit.transport.ssh.commands.DispatchCommand;
3940
import com.gitblit.transport.ssh.commands.SshCommandFactory;
@@ -45,19 +46,20 @@
4546
*/
4647
public class WelcomeShell implements Factory<Command> {
4748

48-
private final IStoredSettings settings;
49+
private final IGitblit gitblit;
4950

50-
public WelcomeShell(IStoredSettings settings) {
51-
this.settings = settings;
51+
public WelcomeShell(IGitblit gitblit) {
52+
this.gitblit = gitblit;
5253
}
5354

5455
@Override
5556
public Command create() {
56-
return new SendMessage(settings);
57+
return new SendMessage(gitblit);
5758
}
5859

5960
private static class SendMessage implements Command, SessionAware {
6061

62+
private final IPublicKeyManager km;
6163
private final IStoredSettings settings;
6264
private ServerSession session;
6365

@@ -66,8 +68,9 @@ private static class SendMessage implements Command, SessionAware {
6668
private OutputStream err;
6769
private ExitCallback exit;
6870

69-
SendMessage(IStoredSettings settings) {
70-
this.settings = settings;
71+
SendMessage(IGitblit gitblit) {
72+
this.km = gitblit.getPublicKeyManager();
73+
this.settings = gitblit.getSettings();
7174
}
7275

7376
@Override
@@ -116,6 +119,10 @@ String getMessage() {
116119
UserModel user = client.getUser();
117120
String hostname = getHostname();
118121
int port = settings.getInteger(Keys.git.sshPort, 0);
122+
boolean writeKeysIsSupported = true;
123+
if (km != null) {
124+
writeKeysIsSupported = km.supportsWritingKeys(user);
125+
}
119126

120127
final String b1 = StringUtils.rightPad("", 72, '═');
121128
final String b2 = StringUtils.rightPad("", 72, '─');
@@ -159,7 +166,7 @@ String getMessage() {
159166
msg.append(nl);
160167
msg.append(nl);
161168

162-
if (client.getKey() == null) {
169+
if (writeKeysIsSupported && client.getKey() == null) {
163170
// user has authenticated with a password
164171
// display add public key instructions
165172
msg.append(" You may upload an SSH public key with the following syntax:");

src/main/java/com/gitblit/transport/ssh/keys/KeysDispatcher.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.slf4j.LoggerFactory;
2626

2727
import com.gitblit.Constants.AccessPermission;
28+
import com.gitblit.models.UserModel;
2829
import com.gitblit.transport.ssh.IPublicKeyManager;
2930
import com.gitblit.transport.ssh.SshKey;
3031
import com.gitblit.transport.ssh.commands.CommandMetaData;
@@ -47,12 +48,20 @@ public class KeysDispatcher extends DispatchCommand {
4748

4849
@Override
4950
protected void setup() {
50-
register(AddKey.class);
51-
register(RemoveKey.class);
51+
IPublicKeyManager km = getContext().getGitblit().getPublicKeyManager();
52+
UserModel user = getContext().getClient().getUser();
53+
if (km != null && km.supportsWritingKeys(user)) {
54+
register(AddKey.class);
55+
register(RemoveKey.class);
56+
}
5257
register(ListKeys.class);
5358
register(WhichKey.class);
54-
register(CommentKey.class);
55-
register(PermissionKey.class);
59+
if (km != null && km.supportsCommentChanges(user)) {
60+
register(CommentKey.class);
61+
}
62+
if (km != null && km.supportsPermissionChanges(user)) {
63+
register(PermissionKey.class);
64+
}
5665
}
5766

5867
@CommandMetaData(name = "add", description = "Add an SSH public key to your account")

src/main/java/com/gitblit/wicket/panels/SshKeysPanel.java

+9
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ public class SshKeysPanel extends BasePanel {
4848
private static final long serialVersionUID = 1L;
4949

5050
private final UserModel user;
51+
private final boolean canWriteKeys;
5152

5253
public SshKeysPanel(String wicketId, UserModel user) {
5354
super(wicketId);
5455

5556
this.user = user;
57+
this.canWriteKeys = app().keys().supportsWritingKeys(user);
5658
}
5759

5860
@Override
@@ -90,6 +92,9 @@ public void onClick(AjaxRequestTarget target) {
9092
}
9193
}
9294
};
95+
if (!canWriteKeys) {
96+
delete.setVisibilityAllowed(false);
97+
}
9398
item.add(delete);
9499
}
95100
};
@@ -164,6 +169,10 @@ protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
164169
}
165170
});
166171

172+
if (! canWriteKeys) {
173+
addKeyForm.setVisibilityAllowed(false);
174+
}
175+
167176
add(addKeyForm);
168177
}
169178
}

0 commit comments

Comments
 (0)