Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dex
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hdacloud
dex
Commits
f46adb95
Unverified
Commit
f46adb95
authored
7 months ago
by
Maksim Nabokikh
Committed by
GitHub
7 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Support base64 encoded and PEM encoded certs (#3751)
Signed-off-by:
maksim.nabokikh
<
max.nabokih@gmail.com
>
parent
4bb97c73
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pkg/httpclient/httpclient.go
+25
-6
25 additions, 6 deletions
pkg/httpclient/httpclient.go
pkg/httpclient/httpclient_test.go
+24
-9
24 additions, 9 deletions
pkg/httpclient/httpclient_test.go
with
49 additions
and
15 deletions
pkg/httpclient/httpclient.go
+
25
−
6
View file @
f46adb95
...
@@ -3,6 +3,7 @@ package httpclient
...
@@ -3,6 +3,7 @@ package httpclient
import
(
import
(
"crypto/tls"
"crypto/tls"
"crypto/x509"
"crypto/x509"
"encoding/base64"
"fmt"
"fmt"
"net"
"net"
"net/http"
"net/http"
...
@@ -10,6 +11,26 @@ import (
...
@@ -10,6 +11,26 @@ import (
"time"
"time"
)
)
func
extractCAs
(
input
[]
string
)
[][]
byte
{
result
:=
make
([][]
byte
,
0
,
len
(
input
))
for
_
,
ca
:=
range
input
{
if
ca
==
""
{
continue
}
pemData
,
err
:=
os
.
ReadFile
(
ca
)
if
err
!=
nil
{
pemData
,
err
=
base64
.
StdEncoding
.
DecodeString
(
ca
)
if
err
!=
nil
{
pemData
=
[]
byte
(
ca
)
}
}
result
=
append
(
result
,
pemData
)
}
return
result
}
func
NewHTTPClient
(
rootCAs
[]
string
,
insecureSkipVerify
bool
)
(
*
http
.
Client
,
error
)
{
func
NewHTTPClient
(
rootCAs
[]
string
,
insecureSkipVerify
bool
)
(
*
http
.
Client
,
error
)
{
pool
,
err
:=
x509
.
SystemCertPool
()
pool
,
err
:=
x509
.
SystemCertPool
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -17,13 +38,11 @@ func NewHTTPClient(rootCAs []string, insecureSkipVerify bool) (*http.Client, err
...
@@ -17,13 +38,11 @@ func NewHTTPClient(rootCAs []string, insecureSkipVerify bool) (*http.Client, err
}
}
tlsConfig
:=
tls
.
Config
{
RootCAs
:
pool
,
InsecureSkipVerify
:
insecureSkipVerify
}
tlsConfig
:=
tls
.
Config
{
RootCAs
:
pool
,
InsecureSkipVerify
:
insecureSkipVerify
}
for
_
,
rootCA
:=
range
rootCAs
{
for
index
,
rootCABytes
:=
range
extractCAs
(
rootCAs
)
{
rootCABytes
,
err
:=
os
.
ReadFile
(
rootCA
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to read root-ca: %v"
,
err
)
}
if
!
tlsConfig
.
RootCAs
.
AppendCertsFromPEM
(
rootCABytes
)
{
if
!
tlsConfig
.
RootCAs
.
AppendCertsFromPEM
(
rootCABytes
)
{
return
nil
,
fmt
.
Errorf
(
"no certs found in root CA file %q"
,
rootCA
)
return
nil
,
fmt
.
Errorf
(
"rootCAs.%d is not in PEM format, certificate must be "
+
"a PEM encoded string, a base64 encoded bytes that contain PEM encoded string, "
+
"or a path to a PEM encoded certificate"
,
index
)
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
pkg/httpclient/httpclient_test.go
+
24
−
9
View file @
f46adb95
...
@@ -2,10 +2,12 @@ package httpclient_test
...
@@ -2,10 +2,12 @@ package httpclient_test
import
(
import
(
"crypto/tls"
"crypto/tls"
"encoding/base64"
"fmt"
"fmt"
"io"
"io"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
"os"
"testing"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
...
@@ -20,18 +22,31 @@ func TestRootCAs(t *testing.T) {
...
@@ -20,18 +22,31 @@ func TestRootCAs(t *testing.T) {
assert
.
Nil
(
t
,
err
)
assert
.
Nil
(
t
,
err
)
defer
ts
.
Close
()
defer
ts
.
Close
()
rootCAs
:=
[]
string
{
"testdata/rootCA.pem"
}
runTest
:=
func
(
name
string
,
certs
[]
string
)
{
testClient
,
err
:=
httpclient
.
NewHTTPClient
(
rootCAs
,
false
)
t
.
Run
(
name
,
func
(
t
*
testing
.
T
)
{
assert
.
Nil
(
t
,
err
)
rootCAs
:=
certs
testClient
,
err
:=
httpclient
.
NewHTTPClient
(
rootCAs
,
false
)
assert
.
Nil
(
t
,
err
)
res
,
err
:=
testClient
.
Get
(
ts
.
URL
)
res
,
err
:=
testClient
.
Get
(
ts
.
URL
)
assert
.
Nil
(
t
,
err
)
assert
.
Nil
(
t
,
err
)
greeting
,
err
:=
io
.
ReadAll
(
res
.
Body
)
greeting
,
err
:=
io
.
ReadAll
(
res
.
Body
)
res
.
Body
.
Close
()
res
.
Body
.
Close
()
assert
.
Nil
(
t
,
err
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
"Hello, client"
,
string
(
greeting
))
assert
.
Equal
(
t
,
"Hello, client"
,
string
(
greeting
))
})
}
runTest
(
"From file"
,
[]
string
{
"testdata/rootCA.pem"
})
content
,
err
:=
os
.
ReadFile
(
"testdata/rootCA.pem"
)
assert
.
NoError
(
t
,
err
)
runTest
(
"From string"
,
[]
string
{
string
(
content
)})
contentStr
:=
base64
.
StdEncoding
.
EncodeToString
(
content
)
runTest
(
"From bytes"
,
[]
string
{
contentStr
})
}
}
func
TestInsecureSkipVerify
(
t
*
testing
.
T
)
{
func
TestInsecureSkipVerify
(
t
*
testing
.
T
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment