Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
mpcs
dfss
Commits
5dc7d485
Commit
5dc7d485
authored
Jan 26, 2016
by
Loïck Bonniot
Browse files
[net] Add the ability to handle non-auth users
- TLS security is not degraded - Unable to create unit tests
parent
f9d27003
Pipeline
#168
passed with stage
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
net/server.go
View file @
5dc7d485
...
...
@@ -3,12 +3,13 @@ package net
import
(
"crypto/tls"
"crypto/x509"
"log"
"net"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/peer"
"log"
"net"
)
// NewServer creates a new grpc server with given tls credentials.
...
...
@@ -33,7 +34,7 @@ func NewServer(cert, key, ca []byte) *grpc.Server {
Certificates
:
[]
tls
.
Certificate
{
serverCert
},
RootCAs
:
caCertPool
,
ClientCAs
:
caCertPool
,
ClientAuth
:
tls
.
RequireAnd
VerifyClientCert
,
ClientAuth
:
tls
.
VerifyClientCert
IfGiven
,
})
opts
=
[]
grpc
.
ServerOption
{
grpc
.
Creds
(
ta
)}
...
...
@@ -55,3 +56,23 @@ func Listen(addrPort string, grpcServer *grpc.Server) {
grpclog
.
Fatalf
(
"Failed to bind gRPC server: %v"
,
err
)
}
}
// GetTLSState returns the current tls connection state from a grpc context.
// If you just need to check that the connected peer provides its certificate, use `GetCN`.
func
GetTLSState
(
ctx
*
context
.
Context
)
(
tls
.
ConnectionState
,
bool
)
{
p
,
ok
:=
peer
.
FromContext
(
*
ctx
)
if
!
ok
{
return
tls
.
ConnectionState
{},
false
}
return
p
.
AuthInfo
.
(
credentials
.
TLSInfo
)
.
State
,
true
}
// GetCN returns the current common name of connected peer from grpc context.
// The returned string is empty if encountering a non-auth peer.
func
GetCN
(
ctx
*
context
.
Context
)
string
{
state
,
ok
:=
GetTLSState
(
ctx
)
if
!
ok
||
len
(
state
.
VerifiedChains
)
==
0
{
return
""
}
return
state
.
VerifiedChains
[
0
][
0
]
.
Subject
.
CommonName
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment