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.pgm; 16 : 17 : import com.google.common.base.Splitter; 18 : import com.google.gerrit.extensions.config.FactoryModule; 19 : import com.google.gerrit.pgm.init.api.ConsoleUI; 20 : import com.google.gerrit.pgm.init.api.InstallAllPlugins; 21 : import com.google.gerrit.pgm.init.api.InstallPlugins; 22 : import com.google.gerrit.pgm.init.api.Section; 23 : import com.google.gerrit.pgm.util.SiteProgram; 24 : import com.google.gerrit.server.config.GerritServerConfigModule; 25 : import com.google.gerrit.server.config.SitePath; 26 : import com.google.gerrit.server.securestore.SecureStoreClassName; 27 : import com.google.inject.Guice; 28 : import com.google.inject.Injector; 29 : import com.google.inject.Module; 30 : import com.google.inject.TypeLiteral; 31 : import com.google.inject.util.Providers; 32 : import java.nio.file.Path; 33 : import java.util.ArrayList; 34 : import java.util.List; 35 : import org.kohsuke.args4j.Argument; 36 : 37 0 : public class Passwd extends SiteProgram { 38 : private String section; 39 : private String key; 40 : 41 : @Argument( 42 : metaVar = "SECTION.KEY", 43 : index = 0, 44 : required = true, 45 : usage = "Section and key separated by a dot of the password to set") 46 : private String sectionAndKey; 47 : 48 : @Argument(metaVar = "PASSWORD", index = 1, required = false, usage = "Password to set") 49 : private String password; 50 : 51 : private void init() { 52 0 : List<String> varParts = Splitter.on('.').splitToList(sectionAndKey); 53 0 : if (varParts.size() != 2) { 54 0 : throw new IllegalArgumentException( 55 : "Invalid name '" + sectionAndKey + "': expected section.key format"); 56 : } 57 0 : section = varParts.get(0); 58 0 : key = varParts.get(1); 59 0 : } 60 : 61 : @Override 62 : public int run() throws Exception { 63 0 : init(); 64 0 : SetPasswd setPasswd = getSysInjector().getInstance(SetPasswd.class); 65 0 : setPasswd.run(section, key, password); 66 0 : return 0; 67 : } 68 : 69 : private Injector getSysInjector() { 70 0 : List<Module> modules = new ArrayList<>(); 71 0 : modules.add( 72 0 : new FactoryModule() { 73 : @Override 74 : protected void configure() { 75 0 : bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath()); 76 0 : bind(ConsoleUI.class).toInstance(ConsoleUI.getInstance(password != null)); 77 0 : factory(Section.Factory.class); 78 0 : bind(Boolean.class).annotatedWith(InstallAllPlugins.class).toInstance(Boolean.FALSE); 79 0 : bind(new TypeLiteral<List<String>>() {}) 80 0 : .annotatedWith(InstallPlugins.class) 81 0 : .toInstance(new ArrayList<>()); 82 0 : bind(String.class) 83 0 : .annotatedWith(SecureStoreClassName.class) 84 0 : .toProvider(Providers.of(getConfiguredSecureStoreClass())); 85 0 : } 86 : }); 87 0 : modules.add(new GerritServerConfigModule()); 88 0 : return Guice.createInjector(modules); 89 : } 90 : }