Skip to content

Commit

Permalink
Merge pull request #95 from keboola/adamvyborny-DBT-26
Browse files Browse the repository at this point in the history
Escape username and password in repository URL
  • Loading branch information
AdamVyborny authored Oct 12, 2023
2 parents 489890c + 5fd1165 commit 8a3e4ed
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/Service/GitRepositoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,7 @@ public function clone(
$branchArgument = ['-b', $branch];
}

$parsedUrl = parse_url($repositoryUrl);
if (!$parsedUrl || !array_key_exists('host', $parsedUrl)) {
throw new UserException(sprintf('Wrong URL format "%s"', $repositoryUrl));
}
if ($username && $password) {
$url = str_replace($parsedUrl['host'], sprintf(
'%s:%s@%s',
$username,
$password,
$parsedUrl['host']
), $repositoryUrl);
} else {
$url = $repositoryUrl;
}
$url = $this->getUrl($repositoryUrl, $username, $password);

try {
$process = new Process(['git', 'clone', ...$branchArgument, $url, 'dbt-project'], $this->dataDir);
Expand Down Expand Up @@ -121,4 +108,27 @@ public function getCurrentBranch(string $projectPath): array
'ref' => trim($process->getOutput()),
];
}

/**
* @throws \Keboola\Component\UserException
*/
public function getUrl(string $repositoryUrl, ?string $username = null, ?string $password = null): string
{
$parsedUrl = parse_url($repositoryUrl);
if (!$parsedUrl || !array_key_exists('host', $parsedUrl)) {
throw new UserException(sprintf('Wrong URL format "%s"', $repositoryUrl));
}
if ($username && $password) {
$url = (string) str_replace($parsedUrl['host'], sprintf(
'%s:%s@%s',
urlencode($username),
urlencode($password),
$parsedUrl['host']
), $repositoryUrl);
} else {
$url = $repositoryUrl;
}

return $url;
}
}
50 changes: 50 additions & 0 deletions tests/phpunit/Service/GitRepositoryServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ public function testListBranchesEmptyGitProject(): void
self::assertEmpty($branches);
}

/**
* @dataProvider getUrlValidParameters
*/
public function testGetRepositoryUrl(
string $url,
?string $username,
?string $password,
string $expectedUrl
): void {
$gitService = new GitRepositoryService($this->dataDir);
self::assertSame($expectedUrl, $gitService->getUrl($url, $username, $password));
}

public function testGetInvalidRepositoryUrl(): void
{
$this->expectException(UserException::class);
$this->expectExceptionMessage('Wrong URL format "invalid url"');
$gitService = new GitRepositoryService($this->dataDir);
$gitService->getUrl('invalid url');
}

/**
* @dataProvider privateRepositoryInvalidCredentials
*/
Expand Down Expand Up @@ -156,4 +177,33 @@ public function privateRepositoryInvalidCredentials(): Generator
'errorMsg' => 'Wrong URL format "some random string"',
];
}

public function getUrlValidParameters(): Generator
{
yield 'public repository' => [
'url' => 'https://github.com/keboola/dbt-test-project-public',
'username' => null,
'password' => null,
'expectedUrl' => 'https://github.com/keboola/dbt-test-project-public',
];

yield 'private repository' => [
'url' => 'https://github.com/keboola/dbt-test-project.git',
'username' => getenv('GITHUB_USERNAME') ?: '',
'password' => getenv('GITHUB_PASSWORD') ?: '',
'expectedUrl' => sprintf(
'https://%s:%s@%s',
getenv('GITHUB_USERNAME') ?: '',
getenv('GITHUB_PASSWORD') ?: '',
'github.com/keboola/dbt-test-project.git',
),
];

yield 'private repository with special chars' => [
'url' => 'https://github.com/keboola/dbt-test-project.git',
'username' => '[email protected]',
'password' => '/some@password!',
'expectedUrl' => 'https://user%40company.com:%2Fsome%40password%[email protected]/keboola/dbt-test-project.git',
];
}
}

0 comments on commit 8a3e4ed

Please sign in to comment.