Line data Source code
1 : // Copyright (C) 2016 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.init; 16 : 17 : import static com.google.common.base.Preconditions.checkState; 18 : 19 : import com.google.common.base.Strings; 20 : import com.google.gerrit.entities.Account; 21 : import com.google.gerrit.entities.RefNames; 22 : import com.google.gerrit.pgm.init.api.AllUsersNameOnInitProvider; 23 : import com.google.gerrit.pgm.init.api.InitFlags; 24 : import com.google.gerrit.pgm.init.api.VersionedMetaDataOnInit; 25 : import com.google.gerrit.server.account.AccountSshKey; 26 : import com.google.gerrit.server.account.AuthorizedKeys; 27 : import com.google.gerrit.server.account.VersionedAuthorizedKeys; 28 : import com.google.gerrit.server.config.SitePaths; 29 : import com.google.inject.Inject; 30 : import com.google.inject.assistedinject.Assisted; 31 : import java.io.IOException; 32 : import java.util.List; 33 : import java.util.Optional; 34 : import org.eclipse.jgit.errors.ConfigInvalidException; 35 : import org.eclipse.jgit.lib.CommitBuilder; 36 : 37 : public class VersionedAuthorizedKeysOnInit extends VersionedMetaDataOnInit { 38 : public interface Factory { 39 : VersionedAuthorizedKeysOnInit create(Account.Id accountId); 40 : } 41 : 42 : private final Account.Id accountId; 43 : private List<Optional<AccountSshKey>> keys; 44 : 45 : @Inject 46 : public VersionedAuthorizedKeysOnInit( 47 : AllUsersNameOnInitProvider allUsers, 48 : SitePaths site, 49 : InitFlags flags, 50 : @Assisted Account.Id accountId) { 51 0 : super(flags, site, allUsers.get(), RefNames.refsUsers(accountId)); 52 0 : this.accountId = accountId; 53 0 : } 54 : 55 : @Override 56 : public VersionedAuthorizedKeysOnInit load() throws IOException, ConfigInvalidException { 57 0 : super.load(); 58 0 : return this; 59 : } 60 : 61 : @Override 62 : protected void onLoad() throws IOException, ConfigInvalidException { 63 0 : keys = AuthorizedKeys.parse(accountId, readUTF8(AuthorizedKeys.FILE_NAME)); 64 0 : } 65 : 66 : public AccountSshKey addKey(String pub) { 67 0 : checkState(keys != null, "SSH keys not loaded yet"); 68 0 : int seq = keys.isEmpty() ? 1 : keys.size() + 1; 69 0 : AccountSshKey key = 70 0 : new VersionedAuthorizedKeys.SimpleSshKeyCreator().create(accountId, seq, pub); 71 0 : keys.add(Optional.of(key)); 72 0 : return key; 73 : } 74 : 75 : @Override 76 : protected boolean onSave(CommitBuilder commit) throws IOException { 77 0 : if (Strings.isNullOrEmpty(commit.getMessage())) { 78 0 : commit.setMessage("Updated SSH keys\n"); 79 : } 80 : 81 0 : saveUTF8(AuthorizedKeys.FILE_NAME, AuthorizedKeys.serialize(keys)); 82 0 : return true; 83 : } 84 : }