Skip to content

Commit 652e3e4

Browse files
committed
Add SSL verification flag to curl
1 parent d318070 commit 652e3e4

File tree

5 files changed

+56
-30
lines changed

5 files changed

+56
-30
lines changed

assets/check

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ cat > "$payload" <&0
2525

2626
log "Configuring git credentials"
2727
load_pubkey "$payload"
28-
configure_git_ssl_verification "$payload"
28+
2929
configure_credentials "$payload"
3030

3131
log "Parsing payload"
3232
uri=$(jq -r '.source.uri // ""' < "$payload")
33+
skip_ssl_verification=$(jq -r '.source.skip_ssl_verification // false' < ${payload})
3334
git_config_payload=$(jq -r '.source.git_config // []' < "$payload")
3435

3536
only_for_branch=$(jq -r '.source.only_for_branch // "."' < "$payload")
@@ -39,6 +40,7 @@ only_when_asked=$(jq -r '.source.only_when_asked // "false"' < "$payload")
3940
rebuild_when_target_changed=$(jq -r '.source.rebuild_when_target_changed // "false"' < "$payload")
4041
rebuild_phrase=$(jq -r '.source.rebuild_phrase // "test this please"' < "$payload")
4142

43+
configure_git_ssl_verification "$skip_ssl_verification"
4244
configure_git_global "${git_config_payload}"
4345

4446
if [ -z "$uri" ]; then
@@ -87,12 +89,19 @@ if [ -n "$pull_requests" ]; then
8789
prq_hash=$(echo "$pull_request" | awk '{print $1}')
8890

8991
# verify target branch of prq
90-
prq=$(bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$prq_number")
92+
prq=$(bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification")
93+
94+
if [ "$prq" = "ERROR" ]; then
95+
continue
96+
fi
97+
98+
log "Pull request #${prq_number}"
99+
91100
prq_to_branch=$(echo "$prq" | jq -r '.toRef.displayId')
92101
if [[ "$prq_to_branch" =~ $only_for_branch ]]; then
93102

94103
if [ "$only_when_mergeable" == "true" -o "$only_without_conflicts" == "true" ]; then
95-
prq_merge=$(bitbucket_pullrequest_merge "$repo_host" "$repo_project" "$repo_name" "$prq_number")
104+
prq_merge=$(bitbucket_pullrequest_merge "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification")
96105

97106
# verify if prq has merge conflicts
98107
conflicted=$(echo "$prq_merge" | jq -r '.conflicted')
@@ -106,7 +115,7 @@ if [ -n "$pull_requests" ]; then
106115
# edit timestamp to version to force new build when rebuild_phrase is included in comments
107116
prq_verify_date=$(echo "$prq" | jq -r '.createdDate')
108117
skip_build=false
109-
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" | jq -c '.[]')
118+
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification" | jq -c '.[]')
110119
if [ -n "$comments" ]; then
111120
while read -r comment; do
112121
text=$(echo "$comment" | jq -r '.text')

assets/helpers/bitbucket.sh

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ bitbucket_request() {
1313
# $3: query
1414
# $4: data
1515
# $5: url base path
16-
# $6: netrc file (default: $HOME/.netrc)
17-
# $7: HTTP method (default: POST for data, GET without data)
18-
# $8: recursive data for bitbucket paging
16+
# $6: Skip SSL verification
17+
# $7: netrc file (default: $HOME/.netrc)
18+
# $8: HTTP method (default: POST for data, GET without data)
19+
# $9: recursive data for bitbucket paging
1920

2021
local data="$4"
2122
local path=${5:-rest/api/1.0}
22-
local netrc_file=${6:-$HOME/.netrc}
23-
local method="$7"
24-
local recursive=${8:-limit=${VALUES_LIMIT}}
23+
local skip_ssl_verification=${6:-"false"}
24+
local netrc_file=${7:-$HOME/.netrc}
25+
local method="$8"
26+
local recursive=${9:-limit=${VALUES_LIMIT}}
2527

2628
local request_url="${1}/${path}/${2}?${recursive}&${3}"
2729
local request_result=$(tmp_file_unique bitbucket-request)
@@ -47,6 +49,10 @@ bitbucket_request() {
4749
extra_options+=" -X $method"
4850
fi
4951

52+
if [ "$skip_ssl_verification" = "true" ]; then
53+
extra_options+=" -k"
54+
fi
55+
5056
curl_cmd="curl -s --netrc-file \"$netrc_file\" $extra_options \"$request_url\" > \"$request_result\""
5157
if ! eval $curl_cmd; then
5258
log "Bitbucket request $request_url failed"
@@ -60,12 +66,15 @@ bitbucket_request() {
6066

6167
if [ "$(jq -r '.isLastPage' < "$request_result")" == "false" ]; then
6268
local nextPage=$(jq -r '.nextPageStart' < "$request_result")
63-
local nextResult=$(bitbucket_request "$1" "$2" "$3" "$4" "$5" "$6" "$7" "start=${nextPage}&limit=${VALUES_LIMIT}")
69+
local nextResult=$(bitbucket_request "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "start=${nextPage}&limit=${VALUES_LIMIT}")
6470
jq -c '.values' < "$request_result" | jq -c ". + $nextResult"
6571
elif [ "$(jq -c '.values' < "$request_result")" != "null" ]; then
6672
jq -c '.values' < "$request_result"
6773
elif [ "$(jq -c '.errors' < "$request_result")" == "null" ]; then
6874
jq '.' < "$request_result"
75+
elif [ "${request_result/NoSuchPullRequestException}" = "${request_result}" ]; then
76+
printf "ERROR"
77+
return
6978
else
7079
log "Bitbucket request ($request_url) failed: $(cat $request_result)"
7180
exit 1
@@ -81,8 +90,9 @@ bitbucket_pullrequest() {
8190
# $3: repository id
8291
# $4: pullrequest id
8392
# $5: netrc file (default: $HOME/.netrc)
93+
# $6: skip ssl verification
8494
log "Retrieving pull request #$4 for $2/$3"
85-
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4" "" "" "" "$5"
95+
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4" "" "" "" "$6" "$5"
8696
}
8797

8898
bitbucket_pullrequest_merge() {
@@ -91,8 +101,9 @@ bitbucket_pullrequest_merge() {
91101
# $3: repository id
92102
# $4: pullrequest id
93103
# $5: netrc file (default: $HOME/.netrc)
104+
# $6: skip ssl verification
94105
log "Retrieving pull request merge status #$4 for $2/$3"
95-
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/merge" "" "" "" "$5"
106+
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/merge" "" "" "" "$6" "$5"
96107
}
97108

98109
bitbucket_pullrequest_overview_comments() {
@@ -101,8 +112,10 @@ bitbucket_pullrequest_overview_comments() {
101112
# $3: repository id
102113
# $4: pullrequest id
103114
# $5: netrc file (default: $HOME/.netrc)
115+
# $6: skip ssl verification
116+
104117
log "Retrieving pull request comments #$4 for $2/$3"
105-
set -o pipefail; bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/activities" "" "" "" "$5" | \
118+
set -o pipefail; bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/activities" "" "" "" "$6" "$5" | \
106119
jq 'map(select(.action == "COMMENTED" and .commentAction == "ADDED" and .commentAnchor == null)) |
107120
sort_by(.createdDate) | reverse |
108121
map({ id: .comment.id, version: .comment.version, text: .comment.text, createdDate: .comment.createdDate })'
@@ -162,9 +175,10 @@ bitbucket_pullrequest_commit_status() {
162175
# $1: host
163176
# $2: commit
164177
# $3: data
165-
# $4: netrc file (default: $HOME/.netrc)
178+
# $5: netrc file (default: $HOME/.netrc)
179+
# $6: skip ssl verification
166180
log "Setting pull request status $2"
167-
bitbucket_request "$1" "commits/$2" "" "$3" "rest/build-status/1.0" "$5"
181+
bitbucket_request "$1" "commits/$2" "" "$3" "rest/build-status/1.0" "$6" "$5"
168182
}
169183

170184
bitbucket_pullrequest_add_comment_status() {
@@ -174,8 +188,9 @@ bitbucket_pullrequest_add_comment_status() {
174188
# $4: pullrequest id
175189
# $5: comment
176190
# $6: netrc file (default: $HOME/.netrc)
191+
# $7: skip ssl verification
177192
log "Adding pull request comment for status on #$4 for $2/$3"
178-
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/comments" "" "{\"text\": \"$5\" }" "" "$6"
193+
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/comments" "" "{\"text\": \"$5\" }" "" "$7" "$6"
179194
}
180195

181196
bitbucket_pullrequest_update_comment_status() {
@@ -187,6 +202,7 @@ bitbucket_pullrequest_update_comment_status() {
187202
# $6: comment id
188203
# $7: comment version
189204
# $8: netrc file (default: $HOME/.netrc)
205+
# $9: skip ssl verification
190206
log "Updating pull request comment (id: $6) for status on #$4 for $2/$3"
191-
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/comments/$6" "" "{\"text\": \"$5\", \"version\": \"$7\" }" "" "$8" "PUT"
207+
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/comments/$6" "" "{\"text\": \"$5\", \"version\": \"$7\" }" "" "$9" "$8" "PUT"
192208
}

assets/helpers/git.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ configure_git_global() {
2727
}
2828

2929
configure_git_ssl_verification() {
30-
skip_ssl_verification=$(jq -r '.source.skip_ssl_verification // false' < $1)
31-
if [ "$skip_ssl_verification" = "true" ]; then
30+
if [ "$1" = "true" ]; then
3231
export GIT_SSL_NO_VERIFY=true
3332
fi
3433
}
@@ -43,7 +42,7 @@ add_pullrequest_metadata_basic() {
4342
uri_parser "$2"
4443
local repo_host="${uri_schema}://${uri_address}"
4544

46-
local title=$(set -o pipefail; bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$1" | jq -r '.title')
45+
local title=$(set -o pipefail; bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$1" "" "$3" | jq -r '.title')
4746
local commit=$(git rev-parse HEAD)
4847
local author=$(git log -1 --format=format:%an)
4948

@@ -92,7 +91,7 @@ pullrequest_metadata() {
9291
local target_commit=$(git rev-list --parents -1 $(git rev-parse HEAD) | awk '{print $2}')
9392

9493
jq -n "[]" | \
95-
add_pullrequest_metadata_basic "$1" "$2" | \
94+
add_pullrequest_metadata_basic "$1" "$2" "$3" | \
9695
add_pullrequest_metadata_commit "source" "$source_commit" | \
9796
add_pullrequest_metadata_commit "target" "$target_commit"
9897
}

assets/in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ payload=$(tmpfile request)
3131
cat > "$payload" <&0
3232

3333
load_pubkey "$payload"
34-
configure_git_ssl_verification "$payload"
3534
configure_credentials "$payload"
3635

36+
skip_ssl_verification=$(jq -r '.source.skip_ssl_verification // false' < ${payload})
3737
uri=$(jq -r '.source.uri // ""' < "$payload")
3838
git_config_payload=$(jq -r '.source.git_config // []' < "$payload")
3939
commit_verification_key_ids=$(jq -r '(.source.commit_verification_key_ids // [])[]' < "$payload")
@@ -46,6 +46,7 @@ disable_git_lfs=$(jq -r '(.params.disable_git_lfs // false)' < "$payload")
4646

4747
prq_id=$(jq -r '.version.id // ""' < "$payload")
4848

49+
configure_git_ssl_verification "$skip_ssl_verification"
4950
configure_git_global "${git_config_payload}"
5051

5152
if [ -z "$uri" ]; then
@@ -138,5 +139,5 @@ git config --add pullrequest.merge $ref
138139

139140
jq -n "{
140141
version: $(jq '.version' < "$payload"),
141-
metadata: $(pullrequest_metadata "$prq_id" "$uri")
142+
metadata: $(pullrequest_metadata "$prq_id" "$uri" "$skip_ssl_verification")
142143
}" >&3

assets/out

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ payload=$(tmpfile request)
3131
cat > "$payload" <&0
3232

3333
load_pubkey "$payload"
34-
configure_git_ssl_verification "$payload"
3534
configure_credentials "$payload"
3635

36+
skip_ssl_verification=$(jq -r '.source.skip_ssl_verification // false' < ${payload})
3737
uri=$(jq -r '.source.uri // ""' < "$payload")
3838
git_config_payload=$(jq -r '.source.git_config // []' < "$payload")
3939
rebuild_when_target_changed=$(jq -r '.source.rebuild_when_target_changed // "false"' < "$payload")
@@ -42,6 +42,7 @@ rebuild_phrase=$(jq -r '.source.rebuild_phrase // "test this please"' < "$payloa
4242
path=$(jq -r '.params.path // ""' < "$payload")
4343
status=$(jq -r '.params.status // ""' < "$payload")
4444

45+
configure_git_ssl_verification "$skip_ssl_verification"
4546
configure_git_global "${git_config_payload}"
4647

4748
if [ -z "$uri" ]; then
@@ -137,14 +138,14 @@ data=$(jq -cn "{
137138
}")
138139

139140
# set commit build status for source commit
140-
bitbucket_pullrequest_commit_status "$repo_host" "$source_commit" "$data"
141+
bitbucket_pullrequest_commit_status "$repo_host" "$source_commit" "$data" "" "" "$skip_ssl_verification"
141142

142143
# use the current commit timestamp as date
143144
prq_verify_date=$(git log -1 --format=format:%at)
144145

145146
# add comment to pull request to track if build was started/finished
146147
comment_message=$(bitbucket_pullrequest_progress_comment "$status" "$prq_hash" "$source_commit" "$target_commit")
147-
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" | jq -c '.[]')
148+
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification" | jq -c '.[]')
148149
commented=""
149150
skip_verify=false
150151
if [ -n "$comments" ]; then
@@ -156,7 +157,7 @@ if [ -n "$comments" ]; then
156157
# check for progress messages => if pull request number matches then edit comment (instead of creating a new one)
157158
if [ -z "$commented" ]; then
158159
if bitbucket_pullrequest_progress_commit_match "$text" "$prq_hash" "Started"; then
159-
bitbucket_pullrequest_update_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" "$id" "$version" >/dev/null
160+
bitbucket_pullrequest_update_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" "$id" "$version" "" "$skip_ssl_verification" >/dev/null
160161
commented=true
161162
fi
162163
fi
@@ -176,7 +177,7 @@ if [ -n "$comments" ]; then
176177
fi
177178

178179
if [ -z "$commented" ]; then
179-
bitbucket_pullrequest_add_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" >/dev/null
180+
bitbucket_pullrequest_add_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" "" "$skip_ssl_verification" >/dev/null
180181
fi
181182

182183
jq -n "{
@@ -185,5 +186,5 @@ jq -n "{
185186
hash: \"$prq_hash\",
186187
date: \"$(date_from_epoch_seconds "$prq_verify_date")\"
187188
},
188-
metadata: $(pullrequest_metadata "$prq_number" "$uri")
189+
metadata: $(pullrequest_metadata "$prq_number" "$uri" "$skip_ssl_verification")
189190
}" >&3

0 commit comments

Comments
 (0)