summaryrefslogtreecommitdiffstats
path: root/gerrit-sshd/src/main/java/com/google/gerrit/sshd/plugin/LfsPluginAuthCommand.java
blob: b44f0fc751c95d202a496d85c015db2574ab1a0f (plain)
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
// Copyright (C) 2017 The Android Open Source Project
//
// 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.google.gerrit.sshd.plugin;

import com.google.gerrit.extensions.registration.DynamicItem;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.sshd.CommandModule;
import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jgit.lib.Config;
import org.kohsuke.args4j.Argument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LfsPluginAuthCommand extends SshCommand {
  private static final Logger log = LoggerFactory.getLogger(LfsPluginAuthCommand.class);
  private static final String CONFIGURATION_ERROR =
      "Server configuration error: LFS auth over SSH is not properly configured.";

  public interface LfsSshPluginAuth {
    String authenticate(CurrentUser user, List<String> args) throws UnloggedFailure, Failure;
  }

  public static class Module extends CommandModule {
    private final boolean pluginProvided;

    @Inject
    Module(@GerritServerConfig Config cfg) {
      pluginProvided = cfg.getString("lfs", null, "plugin") != null;
    }

    @Override
    protected void configure() {
      if (pluginProvided) {
        command("git-lfs-authenticate").to(LfsPluginAuthCommand.class);
        DynamicItem.itemOf(binder(), LfsSshPluginAuth.class);
      }
    }
  }

  private final DynamicItem<LfsSshPluginAuth> auth;
  private final Provider<CurrentUser> user;

  @Argument(index = 0, multiValued = true, metaVar = "PARAMS")
  private List<String> args = new ArrayList<>();

  @Inject
  LfsPluginAuthCommand(DynamicItem<LfsSshPluginAuth> auth, Provider<CurrentUser> user) {
    this.auth = auth;
    this.user = user;
  }

  @Override
  protected void run() throws UnloggedFailure, Exception {
    LfsSshPluginAuth pluginAuth = auth.get();
    if (pluginAuth == null) {
      log.warn(CONFIGURATION_ERROR);
      throw new UnloggedFailure(1, CONFIGURATION_ERROR);
    }

    stdout.print(pluginAuth.authenticate(user.get(), args));
  }
}