import com.gitblit.git.GitblitUploadPackFactory;
import com.gitblit.git.RepositoryResolver;
import com.gitblit.manager.IGitblit;
+import com.gitblit.transport.ssh.commands.AddKeyCommand;
import com.gitblit.transport.ssh.commands.CreateRepository;
import com.gitblit.transport.ssh.commands.DispatchCommand;
import com.gitblit.transport.ssh.commands.Receive;
+import com.gitblit.transport.ssh.commands.RemoveKeyCommand;
import com.gitblit.transport.ssh.commands.ReviewCommand;
import com.gitblit.transport.ssh.commands.SetAccountCommand;
import com.gitblit.transport.ssh.commands.Upload;
public static enum SshSessionBackend {
MINA, NIO2
}
-
+
/**
* 22: IANA assigned port number for ssh. Note that this is a distinct
* concept from gitblit's default conf for ssh port -- this "default" is
public SshDaemon(IGitblit gitblit, IdGenerator idGenerator) {
this.gitblit = gitblit;
this.injector = ObjectGraph.create(new SshModule());
-
+
IStoredSettings settings = gitblit.getSettings();
int port = settings.getInteger(Keys.git.sshPort, 0);
String bindInterface = settings.getString(Keys.git.sshBindInterface,
backend == SshSessionBackend.MINA
? MinaServiceFactoryFactory.class.getName()
: Nio2ServiceFactoryFactory.class.getName());
-
+
InetSocketAddress addr;
if (StringUtils.isEmpty(bindInterface)) {
addr = new InetSocketAddress(port);
DispatchCommand gitblitCmd = new DispatchCommand();
gitblitCmd.registerCommand(CreateRepository.class);
gitblitCmd.registerCommand(VersionCommand.class);
+ gitblitCmd.registerCommand(AddKeyCommand.class);
+ gitblitCmd.registerCommand(RemoveKeyCommand.class);
gitblitCmd.registerCommand(SetAccountCommand.class);
gitblitCmd.registerCommand(ReviewCommand.class);
}
}
}
-
+
protected IKeyManager getKeyManager() {
IKeyManager keyManager = null;
IStoredSettings settings = gitblit.getSettings();
String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName());
if (StringUtils.isEmpty(clazz)) {
clazz = FileKeyManager.class.getName();
- }
+ }
try {
Class<? extends IKeyManager> managerClass = (Class<? extends IKeyManager>) Class.forName(clazz);
keyManager = injector.get(managerClass).start();
}
return keyManager;
}
-
+
/**
* A nested Dagger graph is used for constructor dependency injection of
* complex classes.
@Provides @Singleton NullKeyManager provideNullKeyManager() {
return new NullKeyManager();
}
-
+
@Provides @Singleton FileKeyManager provideFileKeyManager() {
return new FileKeyManager(SshDaemon.this.gitblit);
}
--- /dev/null
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.gitblit.transport.ssh.commands;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.kohsuke.args4j.Argument;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.gitblit.transport.ssh.CommandMetaData;
+import com.gitblit.transport.ssh.IKeyManager;
+
+/**
+ * Add a key to the current user's authorized keys list.
+ *
+ * @author James Moger
+ *
+ */
+@CommandMetaData(name = "add-key", description = "Add an SSH public key to your account")
+public class AddKeyCommand extends BaseKeyCommand {
+
+ protected final Logger log = LoggerFactory.getLogger(getClass());
+
+ @Argument(metaVar = "<stdin>|KEY", usage = "the key to add")
+ private List<String> addKeys = new ArrayList<String>();
+
+ @Override
+ public void run() throws IOException, UnloggedFailure {
+ String username = ctx.getClient().getUsername();
+ List<String> keys = readKeys(addKeys);
+ IKeyManager keyManager = authenticator.getKeyManager();
+ for (String key : keys) {
+ keyManager.addKey(username, key);
+ log.info("added SSH public key for {}", username);
+ }
+ authenticator.getKeyCache().invalidate(username);
+ }
+}
--- /dev/null
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.gitblit.transport.ssh.commands;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.util.List;
+
+import com.gitblit.transport.ssh.SshKeyAuthenticator;
+import com.google.common.base.Charsets;
+
+/**
+ *
+ * Base class for commands that read SSH keys from stdin or a parameter list.
+ *
+ */
+public abstract class BaseKeyCommand extends SshCommand {
+
+ protected List<String> readKeys(List<String> sshKeys)
+ throws UnsupportedEncodingException, IOException {
+ int idx = -1;
+ if (sshKeys.isEmpty() || ((idx = sshKeys.indexOf("-")) >= 0)) {
+ String sshKey = "";
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ in, Charsets.UTF_8));
+ String line;
+ while ((line = br.readLine()) != null) {
+ sshKey += line + "\n";
+ }
+ if (idx == -1) {
+ sshKeys.add(sshKey.trim());
+ } else {
+ sshKeys.set(idx, sshKey.trim());
+ }
+ }
+ return sshKeys;
+ }
+
+ protected SshKeyAuthenticator authenticator;
+ public void setAuthenticator(SshKeyAuthenticator authenticator) {
+ this.authenticator = authenticator;
+ }
+}
d.setUploadPackFactory(gitblitUploadPackFactory);
d.setReceivePackFactory(gitblitReceivePackFactory);
d.setAuthenticator(authenticator);
- } else if (cmd instanceof SetAccountCommand) {
- SetAccountCommand setAccountCommand = (SetAccountCommand)cmd;
- setAccountCommand.setAuthenticator(authenticator);
+ } else if (cmd instanceof BaseKeyCommand) {
+ BaseKeyCommand k = (BaseKeyCommand)cmd;
+ k.setAuthenticator(authenticator);
}
}
--- /dev/null
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.gitblit.transport.ssh.commands;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.kohsuke.args4j.Argument;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.gitblit.transport.ssh.CommandMetaData;
+import com.gitblit.transport.ssh.IKeyManager;
+
+
+/**
+ * Remove an SSH public key from the current user's authorized key list.
+ *
+ * @author James Moger
+ *
+ */
+@CommandMetaData(name = "rm-key", description = "Remove an SSH public key from your account")
+public class RemoveKeyCommand extends BaseKeyCommand {
+
+ protected final Logger log = LoggerFactory.getLogger(getClass());
+
+ private static final String ALL = "ALL";
+
+ @Argument(metaVar = "<stdin>|<KEY>|ALL", usage = "the key to remove")
+ private List<String> removeKeys = new ArrayList<String>();
+
+ @Override
+ public void run() throws IOException, UnloggedFailure {
+ String username = ctx.getClient().getUsername();
+ List<String> keys = readKeys(removeKeys);
+ IKeyManager keyManager = authenticator.getKeyManager();
+ if (keys.contains(ALL)) {
+ keyManager.removeAllKeys(username);
+ log.info("removed all SSH public keys from {}", username);
+ } else {
+ for (String key : keys) {
+ keyManager.removeKey(username, key);
+ log.info("removed SSH public key from {}", username);
+ }
+ }
+ authenticator.getKeyCache().invalidate(username);
+ }
+}
package com.gitblit.transport.ssh.commands;
-import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.gitblit.transport.ssh.CommandMetaData;
import com.gitblit.transport.ssh.IKeyManager;
-import com.gitblit.transport.ssh.SshKeyAuthenticator;
-import com.google.common.base.Charsets;
/** Set a user's account settings. **/
@CommandMetaData(name = "set-account", description = "Change an account's settings")
-public class SetAccountCommand extends SshCommand {
+public class SetAccountCommand extends BaseKeyCommand {
private static final String ALL = "ALL";
}
private void setAccount() throws IOException, UnloggedFailure {
- addSshKeys = readSshKey(addSshKeys);
+ addSshKeys = readKeys(addSshKeys);
if (!addSshKeys.isEmpty()) {
addSshKeys(addSshKeys);
}
- deleteSshKeys = readSshKey(deleteSshKeys);
+ deleteSshKeys = readKeys(deleteSshKeys);
if (!deleteSshKeys.isEmpty()) {
deleteSshKeys(deleteSshKeys);
}
}
}
}
-
- private List<String> readSshKey(List<String> sshKeys)
- throws UnsupportedEncodingException, IOException {
- if (!sshKeys.isEmpty()) {
- String sshKey;
- int idx = sshKeys.indexOf("-");
- if (idx >= 0) {
- sshKey = "";
- BufferedReader br = new BufferedReader(new InputStreamReader(
- in, Charsets.UTF_8));
- String line;
- while ((line = br.readLine()) != null) {
- sshKey += line + "\n";
- }
- sshKeys.set(idx, sshKey);
- }
- }
- return sshKeys;
- }
-
- private SshKeyAuthenticator authenticator;
- public void setAuthenticator(SshKeyAuthenticator authenticator) {
- this.authenticator = authenticator;
- }
}