Skip to content

Commit

Permalink
Rename MirroringCredential to Credential (#1031)
Browse files Browse the repository at this point in the history
Motivation:
We decided to repurpose the `MirrorCredential` to manage all repository credentials, not just those specific to mirroring. To reflect this role, we should remove `Mirror` prefix from the class name.

Caveat: This commit must be deployed to central dogma replicas after applying the changes from #1030.

Modifications:
- Renamed `MirroringCredential` to `Credential` and moved its package.
- Removed `hostnamePatterns` property in `Credential`.

Result:
- The renamed `Credential` class can now be used for managing various types of repository credentials, beyond just mirroring.
  • Loading branch information
minwoox authored Sep 27, 2024
1 parent 6ee1029 commit b550f27
Show file tree
Hide file tree
Showing 45 changed files with 340 additions and 1,079 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ private void pushCredentials(String pubKey, String privKey) {
'{' +
" \"id\": \"public-key-id\"," +
" \"type\": \"public_key\"," +
" \"hostnamePatterns\": [ \"^.*$\" ]," +
" \"username\": \"" + "git" + "\"," +
" \"publicKey\": \"" + pubKey + "\"," +
" \"privateKey\": \"" + privKey + '"' +
Expand All @@ -225,7 +224,8 @@ private void pushMirror(String gitUri, MirrorDirection mirrorDirection) {
" \"localRepo\": \"" + REPO_FOO + "\"," +
" \"localPath\": \"/\"," +
" \"remoteUri\": \"" + gitUri + "\"," +
" \"schedule\": \"0 0 0 1 1 ? 2099\"" +
" \"schedule\": \"0 0 0 1 1 ? 2099\"," +
" \"credentialId\": \"public-key-id\"" +
'}'))
.push().join();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ void auth(String projName, String gitUri, JsonNode credential) {
" \"direction\": \"REMOTE_TO_LOCAL\"," +
" \"localRepo\": \"main\"," +
" \"localPath\": \"/\"," +
" \"remoteUri\": \"" + gitUri + '"' +
" \"remoteUri\": \"" + gitUri + "\"," +
" \"credentialId\": \"" + credentialId + '"' +
'}'))
.push().join();

Expand All @@ -138,7 +139,6 @@ private static Collection<Arguments> arguments() throws Exception {
" \"id\": \"password-id\"," +
" \"enabled\": true," +
" \"type\": \"password\"," +
" \"hostnamePatterns\": [ \"^.*$\" ]," +
" \"username\": \"" + GITHUB_USERNAME + "\"," +
" \"password\": \"" + Jackson.escapeText(GITHUB_PASSWORD) + '"' +
'}')));
Expand All @@ -153,7 +153,6 @@ private static Collection<Arguments> arguments() throws Exception {
" \"id\": \"access-token-id\"," +
" \"enabled\": true," +
" \"type\": \"access_token\"," +
" \"hostnamePatterns\": [ \"^.*$\" ]," +
" \"accessToken\": \"" + Jackson.escapeText(GITHUB_ACCESS_TOKEN) + '"' +
'}')));
}
Expand Down Expand Up @@ -212,7 +211,6 @@ private static void sshAuth(Builder<Arguments> builder, String privateKeyFile, S
" \"id\": \"" + privateKeyFile + "\"," +
" \"enabled\": true," +
" \"type\": \"public_key\"," +
" \"hostnamePatterns\": [ \"^.*$\" ]," +
" \"username\": \"git\"," +
" \"publicKey\": \"" + Jackson.escapeText(publicKey) + "\"," +
" \"privateKey\": \"" + Jackson.escapeText(privateKey) + "\"," +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
import com.linecorp.centraldogma.server.MirrorException;
import com.linecorp.centraldogma.server.command.Command;
import com.linecorp.centraldogma.server.command.CommandExecutor;
import com.linecorp.centraldogma.server.mirror.MirrorCredential;
import com.linecorp.centraldogma.server.credential.Credential;
import com.linecorp.centraldogma.server.mirror.MirrorDirection;
import com.linecorp.centraldogma.server.storage.StorageException;
import com.linecorp.centraldogma.server.storage.repository.Repository;
Expand Down Expand Up @@ -116,7 +116,7 @@ abstract class AbstractGitMirror extends AbstractMirror {
private IgnoreNode ignoreNode;

AbstractGitMirror(String id, boolean enabled, Cron schedule, MirrorDirection direction,
MirrorCredential credential, Repository localRepo, String localPath,
Credential credential, Repository localRepo, String localPath,
URI remoteRepoUri, String remotePath, String remoteBranch,
@Nullable String gitignore) {
super(id, enabled, schedule, direction, credential, localRepo, localPath, remoteRepoUri, remotePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import com.cronutils.model.Cron;

import com.linecorp.centraldogma.server.command.CommandExecutor;
import com.linecorp.centraldogma.server.internal.mirror.credential.AccessTokenMirrorCredential;
import com.linecorp.centraldogma.server.internal.mirror.credential.PasswordMirrorCredential;
import com.linecorp.centraldogma.server.mirror.MirrorCredential;
import com.linecorp.centraldogma.server.credential.Credential;
import com.linecorp.centraldogma.server.internal.credential.AccessTokenCredential;
import com.linecorp.centraldogma.server.internal.credential.PasswordCredential;
import com.linecorp.centraldogma.server.mirror.MirrorDirection;
import com.linecorp.centraldogma.server.storage.repository.Repository;

Expand All @@ -43,7 +43,7 @@ final class DefaultGitMirror extends AbstractGitMirror {
private static final Consumer<TransportCommand<?, ?>> NOOP_CONFIGURATOR = command -> {};

DefaultGitMirror(String id, boolean enabled, Cron schedule, MirrorDirection direction,
MirrorCredential credential, Repository localRepo, String localPath,
Credential credential, Repository localRepo, String localPath,
URI remoteRepoUri, String remotePath, String remoteBranch,
@Nullable String gitignore) {
super(id, enabled, schedule, direction, credential, localRepo, localPath, remoteRepoUri, remotePath,
Expand All @@ -58,17 +58,17 @@ protected void mirrorLocalToRemote(File workDir, int maxNumFiles, long maxNumByt
}

private Consumer<TransportCommand<?, ?>> transportCommandConfigurator() {
final MirrorCredential c = credential();
final Credential c = credential();
switch (remoteRepoUri().getScheme()) {
case SCHEME_GIT_HTTP:
case SCHEME_GIT_HTTPS:
if (c instanceof PasswordMirrorCredential) {
final PasswordMirrorCredential cred = (PasswordMirrorCredential) c;
if (c instanceof PasswordCredential) {
final PasswordCredential cred = (PasswordCredential) c;
return command -> command.setCredentialsProvider(
new UsernamePasswordCredentialsProvider(cred.username(), cred.password()));
}
if (c instanceof AccessTokenMirrorCredential) {
final AccessTokenMirrorCredential cred = (AccessTokenMirrorCredential) c;
if (c instanceof AccessTokenCredential) {
final AccessTokenCredential cred = (AccessTokenCredential) c;
return command -> command.setCredentialsProvider(
new UsernamePasswordCredentialsProvider("token", cred.accessToken()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.linecorp.centraldogma.server.internal.mirror;

import static com.linecorp.centraldogma.server.internal.mirror.credential.PublicKeyMirrorCredential.publicKeyPreview;
import static com.linecorp.centraldogma.server.internal.credential.PublicKeyCredential.publicKeyPreview;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -57,9 +57,9 @@

import com.linecorp.centraldogma.server.MirrorException;
import com.linecorp.centraldogma.server.command.CommandExecutor;
import com.linecorp.centraldogma.server.internal.mirror.credential.PasswordMirrorCredential;
import com.linecorp.centraldogma.server.internal.mirror.credential.PublicKeyMirrorCredential;
import com.linecorp.centraldogma.server.mirror.MirrorCredential;
import com.linecorp.centraldogma.server.credential.Credential;
import com.linecorp.centraldogma.server.internal.credential.PasswordCredential;
import com.linecorp.centraldogma.server.internal.credential.PublicKeyCredential;
import com.linecorp.centraldogma.server.mirror.MirrorDirection;
import com.linecorp.centraldogma.server.storage.repository.Repository;

Expand All @@ -80,7 +80,7 @@ final class SshGitMirror extends AbstractGitMirror {
private static final BouncyCastleRandom bounceCastleRandom = new BouncyCastleRandom();

SshGitMirror(String id, boolean enabled, Cron schedule, MirrorDirection direction,
MirrorCredential credential, Repository localRepo, String localPath,
Credential credential, Repository localRepo, String localPath,
URI remoteRepoUri, String remotePath, String remoteBranch,
@Nullable String gitignore) {
super(id, enabled, schedule, direction, credential, localRepo, localPath, remoteRepoUri, remotePath,
Expand Down Expand Up @@ -118,10 +118,10 @@ protected void mirrorRemoteToLocal(File workDir, CommandExecutor executor,
private URIish remoteUri() throws URISyntaxException {
// Requires the username to be included in the URI.
final String username;
if (credential() instanceof PasswordMirrorCredential) {
username = ((PasswordMirrorCredential) credential()).username();
} else if (credential() instanceof PublicKeyMirrorCredential) {
username = ((PublicKeyMirrorCredential) credential()).username();
if (credential() instanceof PasswordCredential) {
username = ((PasswordCredential) credential()).username();
} else if (credential() instanceof PublicKeyCredential) {
username = ((PublicKeyCredential) credential()).username();
} else {
username = null;
}
Expand Down Expand Up @@ -181,11 +181,11 @@ static ClientSession createSession(SshClient sshClient, URIish uri) {
}

private void configureCredential(SshClient client) {
final MirrorCredential c = credential();
if (c instanceof PasswordMirrorCredential) {
client.setFilePasswordProvider(passwordProvider(((PasswordMirrorCredential) c).password()));
} else if (c instanceof PublicKeyMirrorCredential) {
final PublicKeyMirrorCredential cred = (PublicKeyMirrorCredential) credential();
final Credential c = credential();
if (c instanceof PasswordCredential) {
client.setFilePasswordProvider(passwordProvider(((PasswordCredential) c).password()));
} else if (c instanceof PublicKeyCredential) {
final PublicKeyCredential cred = (PublicKeyCredential) credential();
final Collection<KeyPair> keyPairs;
try {
keyPairs = keyPairResourceParser.loadKeyPairs(null, NamedResource.ofName(cred.username()),
Expand Down
Loading

0 comments on commit b550f27

Please sign in to comment.