Line data Source code
1 : // Copyright (C) 2017 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.sshd.plugin; 16 : 17 : import com.google.common.flogger.FluentLogger; 18 : import com.google.gerrit.extensions.registration.DynamicItem; 19 : import com.google.gerrit.server.CurrentUser; 20 : import com.google.gerrit.server.config.GerritServerConfig; 21 : import com.google.gerrit.sshd.CommandModule; 22 : import com.google.gerrit.sshd.SshCommand; 23 : import com.google.inject.Inject; 24 : import java.util.ArrayList; 25 : import java.util.List; 26 : import org.eclipse.jgit.lib.Config; 27 : import org.kohsuke.args4j.Argument; 28 : 29 : public class LfsPluginAuthCommand extends SshCommand { 30 0 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 31 : 32 : private static final String CONFIGURATION_ERROR = 33 : "Server configuration error: LFS auth over SSH is not properly configured."; 34 : 35 : public interface LfsSshPluginAuth { 36 : String authenticate(CurrentUser user, List<String> args) throws UnloggedFailure, Failure; 37 : } 38 : 39 : public static class LfsPluginAuthCommandModule extends CommandModule { 40 : private final boolean pluginProvided; 41 : 42 : @Inject 43 17 : LfsPluginAuthCommandModule(@GerritServerConfig Config cfg) { 44 17 : pluginProvided = cfg.getString("lfs", null, "plugin") != null; 45 17 : } 46 : 47 : @Override 48 : protected void configure() { 49 17 : if (pluginProvided) { 50 0 : command("git-lfs-authenticate").to(LfsPluginAuthCommand.class); 51 0 : DynamicItem.itemOf(binder(), LfsSshPluginAuth.class); 52 : } 53 17 : } 54 : } 55 : 56 : private final DynamicItem<LfsSshPluginAuth> auth; 57 : 58 0 : @Argument(index = 0, multiValued = true, metaVar = "PARAMS") 59 : private List<String> args = new ArrayList<>(); 60 : 61 : @Inject 62 0 : LfsPluginAuthCommand(DynamicItem<LfsSshPluginAuth> auth) { 63 0 : this.auth = auth; 64 0 : } 65 : 66 : @Override 67 : protected void run() throws UnloggedFailure, Exception { 68 0 : LfsSshPluginAuth pluginAuth = auth.get(); 69 0 : if (pluginAuth == null) { 70 0 : logger.atWarning().log(CONFIGURATION_ERROR); 71 0 : throw new UnloggedFailure(1, CONFIGURATION_ERROR); 72 : } 73 : 74 0 : stdout.print(pluginAuth.authenticate(user, args)); 75 0 : } 76 : }