Skip to content

Commit 7a2277b

Browse files
committed
Add config for private submodules from external git servers
Signed-off-by: Simeon Korchev <simeonkorchev@gmail.com>
1 parent 1acb6a3 commit 7a2277b

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ Tracks the commits in a [git](http://git-scm.com/) repository.
5252
the `branch`. Patterns are [glob(7)](http://man7.org/linux/man-pages/man7/glob.7.html)
5353
compatible (as in, bash compatible).
5454
55+
* `submodules`: *Optional.* List of credentials for HTTP(s) auth when pulling/pushing private git submodules which are not stored in the same git server as the container repository.
56+
Example:
57+
```
58+
submodules:
59+
- host: github.com
60+
username: git-user
61+
password: git-password
62+
- <another-configuration>
63+
```
64+
Note that `host` is specified with no protocol extensions.
65+
5566
* `git_config`: *Optional.* If specified as (list of pairs `name` and `value`)
5667
it will configure git global options, setting each name with each value.
5768
@@ -124,6 +135,27 @@ resources:
124135
proxy_password: myverysecurepassword
125136
```
126137

138+
Resource configuration for a private repo with a private submodule from different git server:
139+
140+
``` yaml
141+
resources:
142+
- name: source-code
143+
type: git
144+
source:
145+
uri: git@github.com:concourse/git-resource.git
146+
branch: master
147+
submodules:
148+
- host: some.other.git.server
149+
username: user
150+
password: verysecurepassword
151+
private_key: |
152+
-----BEGIN RSA PRIVATE KEY-----
153+
MIIEowIBAAKCAQEAtCS10/f7W7lkQaSgD/mVeaSOvSF9ql4hf/zfMwfVGgHWjj+W
154+
<Lots more text>
155+
DWiJL+OFeg9kawcUL6hQ8JeXPhlImG6RTUffma9+iGQyyBMCGd1l
156+
-----END RSA PRIVATE KEY-----
157+
```
158+
127159
Fetching a repo with only 100 commits of history:
128160
129161
``` yaml

assets/common.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,32 @@ git_metadata() {
160160
add_git_metadata_url
161161
}
162162

163+
configure_submodule_credentials() {
164+
local username
165+
local password
166+
if [[ "$(jq -r '.source.submodules // ""' < "$1")" == "" ]]; then
167+
return
168+
fi
169+
170+
for k in $(jq -r '.source.submodules | keys | .[]' < "$1"); do
171+
host=$(jq -r --argjson k "$k" '.source.submodules[$k].host // ""' < "$1")
172+
username=$(jq -r --argjson k "$k" '.source.submodules[$k].username // ""' < "$1")
173+
password=$(jq -r --argjson k "$k" '.source.submodules[$k].password // ""' < "$1")
174+
if [ "$username" != "" -a "$password" != "" -a "$host" != "" ]; then
175+
echo "machine $host login $username password $password" >> "${HOME}/.netrc"
176+
fi
177+
done
178+
}
179+
163180
configure_credentials() {
164181
local username=$(jq -r '.source.username // ""' < $1)
165182
local password=$(jq -r '.source.password // ""' < $1)
166183

167184
rm -f $HOME/.netrc
185+
configure_submodule_credentials "$1"
186+
168187
if [ "$username" != "" -a "$password" != "" ]; then
169-
echo "default login $username password $password" > $HOME/.netrc
188+
echo "default login $username password $password" >> "${HOME}/.netrc"
170189
fi
171190
}
172191

test/check.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,28 @@ it_can_check_with_credentials() {
8282
[ ! -f "$HOME/.netrc" ]
8383
}
8484

85+
it_can_check_with_submodule_credentials() {
86+
local repo=$(init_repo)
87+
local ref=$(make_commit "$repo")
88+
local expected_netrc
89+
expected_netrc=$(cat <<EOF
90+
machine host1 login user2 password pass2
91+
default login user1 password pass1
92+
EOF
93+
)
94+
check_uri_with_submodule_credentials "$repo" "user1" "pass1" "host1" "user2" "pass2" | jq -e "
95+
. == [{ref: $(echo $ref | jq -R .)}]
96+
"
97+
echo "Generated netrc $(cat ${HOME}/.netrc)"
98+
echo "Expected netrc $expected_netrc"
99+
[ "$(cat $HOME/.netrc)" = "$expected_netrc" ]
100+
101+
check_uri_with_credentials $repo "" "" | jq -e "
102+
. == [{ref: $(echo $ref | jq -R .)}]
103+
"
104+
[ ! -f "$HOME/.netrc" ]
105+
}
106+
85107
it_clears_netrc_even_after_errors() {
86108
local repo=$(init_repo)
87109
local ref=$(make_commit $repo)
@@ -702,6 +724,7 @@ run it_fails_if_key_has_password
702724
run it_configures_forward_agent
703725
run it_skips_forward_agent_configuration
704726
run it_can_check_with_credentials
727+
run it_can_check_with_submodule_credentials
705728
run it_clears_netrc_even_after_errors
706729
run it_can_check_empty_commits
707730
run it_can_check_with_tag_filter

test/helpers.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,22 @@ check_uri_with_credentials() {
293293
}" | ${resource_dir}/check | tee /dev/stderr
294294
}
295295

296+
check_uri_with_submodule_credentials() {
297+
jq -n "{
298+
source: {
299+
uri: $(echo $1 | jq -R .),
300+
username: $(echo $2 | jq -R .),
301+
password: $(echo $3 | jq -R .),
302+
submodules: [
303+
{
304+
host: $(echo $4 | jq -R .),
305+
username: $(echo $5 | jq -R .),
306+
password: $(echo $6 | jq -R .)
307+
}
308+
]
309+
}
310+
}" | ${resource_dir}/check | tee /dev/stderr
311+
}
296312

297313
check_uri_ignoring() {
298314
local uri=$1

0 commit comments

Comments
 (0)