]> source.dussan.org Git - gitblit.git/commitdiff
Add add-key and rm-key commands that apply only to the current user
authorJames Moger <james.moger@gitblit.com>
Fri, 14 Mar 2014 16:20:45 +0000 (12:20 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 10 Apr 2014 22:58:08 +0000 (18:58 -0400)
src/main/java/com/gitblit/transport/ssh/SshDaemon.java
src/main/java/com/gitblit/transport/ssh/commands/AddKeyCommand.java [new file with mode: 0644]
src/main/java/com/gitblit/transport/ssh/commands/BaseKeyCommand.java [new file with mode: 0644]
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
src/main/java/com/gitblit/transport/ssh/commands/RemoveKeyCommand.java [new file with mode: 0644]
src/main/java/com/gitblit/transport/ssh/commands/SetAccountCommand.java

index 81d7878409bb5f0eca582c6560e8a1b49d7358c5..c8c20f56d154cecaaf4b2472920ead9d64222dc7 100644 (file)
@@ -38,9 +38,11 @@ import com.gitblit.git.GitblitReceivePackFactory;
 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;
@@ -67,7 +69,7 @@ public class SshDaemon {
        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
@@ -92,7 +94,7 @@ public class SshDaemon {
        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,
@@ -107,7 +109,7 @@ public class SshDaemon {
                    backend == SshSessionBackend.MINA
                        ? MinaServiceFactoryFactory.class.getName()
                        : Nio2ServiceFactoryFactory.class.getName());
-               
+
                InetSocketAddress addr;
                if (StringUtils.isEmpty(bindInterface)) {
                        addr = new InetSocketAddress(port);
@@ -131,6 +133,8 @@ public class SshDaemon {
                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);
 
@@ -210,14 +214,14 @@ public class SshDaemon {
                        }
                }
        }
-       
+
        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();
@@ -232,7 +236,7 @@ public class SshDaemon {
                }
                return keyManager;
        }
-       
+
        /**
         * A nested Dagger graph is used for constructor dependency injection of
         * complex classes.
@@ -252,7 +256,7 @@ public class SshDaemon {
                @Provides @Singleton NullKeyManager provideNullKeyManager() {
                        return new NullKeyManager();
                }
-               
+
                @Provides @Singleton FileKeyManager provideFileKeyManager() {
                        return new FileKeyManager(SshDaemon.this.gitblit);
                }
diff --git a/src/main/java/com/gitblit/transport/ssh/commands/AddKeyCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/AddKeyCommand.java
new file mode 100644 (file)
index 0000000..69c4fec
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * 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);
+       }
+}
diff --git a/src/main/java/com/gitblit/transport/ssh/commands/BaseKeyCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/BaseKeyCommand.java
new file mode 100644 (file)
index 0000000..50927cc
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * 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;
+       }
+}
index dc963309afb6d846c6fcf36331aa62ce714ef246..672ecd6166c3501e17c512f893872e7e222fded9 100644 (file)
@@ -198,9 +198,9 @@ public class DispatchCommand extends BaseCommand {
                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);
          }
   }
 
diff --git a/src/main/java/com/gitblit/transport/ssh/commands/RemoveKeyCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/RemoveKeyCommand.java
new file mode 100644 (file)
index 0000000..0d49164
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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);
+       }
+}
index 767f3cb41829947da21f270faa5585a6a07f11f4..0eabdce8bf9ae30d9355354e011d0c2a0c173199 100644 (file)
 
 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;
@@ -27,12 +24,10 @@ import org.kohsuke.args4j.Option;
 
 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";
 
@@ -61,12 +56,12 @@ public class SetAccountCommand extends SshCommand {
        }
 
        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);
                }
@@ -91,28 +86,4 @@ public class SetAccountCommand extends SshCommand {
                        }
                }
        }
-
-       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;
-       }
 }