Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
a0cebcb5
Commit
a0cebcb5
authored
Jan 09, 2017
by
zhouhaibing089
Committed by
haibzhou
Aug 05, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
plugin/pkg/client/auth: add openstack auth provider
parent
b6ff77ee
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
348 additions
and
0 deletions
+348
-0
Godeps.json
staging/src/k8s.io/client-go/Godeps/Godeps.json
+28
-0
BUILD
staging/src/k8s.io/client-go/plugin/pkg/client/auth/BUILD
+2
-0
BUILD
...c/k8s.io/client-go/plugin/pkg/client/auth/openstack/BUILD
+40
-0
openstack.go
...o/client-go/plugin/pkg/client/auth/openstack/openstack.go
+161
-0
openstack_test.go
...ent-go/plugin/pkg/client/auth/openstack/openstack_test.go
+116
-0
plugins.go
...ng/src/k8s.io/client-go/plugin/pkg/client/auth/plugins.go
+1
-0
No files found.
staging/src/k8s.io/client-go/Godeps/Godeps.json
View file @
a0cebcb5
...
@@ -175,6 +175,34 @@
...
@@ -175,6 +175,34 @@
"Rev"
:
"0c5108395e2debce0d731cf0287ddf7242066aba"
"Rev"
:
"0c5108395e2debce0d731cf0287ddf7242066aba"
},
},
{
{
"ImportPath"
:
"github.com/gophercloud/gophercloud"
,
"Rev"
:
"ed590d9afe113c6107cd60717b196155e6579e78"
},
{
"ImportPath"
:
"github.com/gophercloud/gophercloud/openstack"
,
"Rev"
:
"ed590d9afe113c6107cd60717b196155e6579e78"
},
{
"ImportPath"
:
"github.com/gophercloud/gophercloud/openstack/identity/v2/tenants"
,
"Rev"
:
"ed590d9afe113c6107cd60717b196155e6579e78"
},
{
"ImportPath"
:
"github.com/gophercloud/gophercloud/openstack/identity/v2/tokens"
,
"Rev"
:
"ed590d9afe113c6107cd60717b196155e6579e78"
},
{
"ImportPath"
:
"github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
,
"Rev"
:
"ed590d9afe113c6107cd60717b196155e6579e78"
},
{
"ImportPath"
:
"github.com/gophercloud/gophercloud/openstack/utils"
,
"Rev"
:
"ed590d9afe113c6107cd60717b196155e6579e78"
},
{
"ImportPath"
:
"github.com/gophercloud/gophercloud/pagination"
,
"Rev"
:
"ed590d9afe113c6107cd60717b196155e6579e78"
},
{
"ImportPath"
:
"github.com/gregjones/httpcache"
,
"ImportPath"
:
"github.com/gregjones/httpcache"
,
"Rev"
:
"787624de3eb7bd915c329cba748687a3b22666a6"
"Rev"
:
"787624de3eb7bd915c329cba748687a3b22666a6"
},
},
...
...
staging/src/k8s.io/client-go/plugin/pkg/client/auth/BUILD
View file @
a0cebcb5
...
@@ -15,6 +15,7 @@ go_library(
...
@@ -15,6 +15,7 @@ go_library(
"//vendor/k8s.io/client-go/plugin/pkg/client/auth/azure:go_default_library",
"//vendor/k8s.io/client-go/plugin/pkg/client/auth/azure:go_default_library",
"//vendor/k8s.io/client-go/plugin/pkg/client/auth/gcp:go_default_library",
"//vendor/k8s.io/client-go/plugin/pkg/client/auth/gcp:go_default_library",
"//vendor/k8s.io/client-go/plugin/pkg/client/auth/oidc:go_default_library",
"//vendor/k8s.io/client-go/plugin/pkg/client/auth/oidc:go_default_library",
"//vendor/k8s.io/client-go/plugin/pkg/client/auth/openstack:go_default_library",
],
],
)
)
...
@@ -32,6 +33,7 @@ filegroup(
...
@@ -32,6 +33,7 @@ filegroup(
"//staging/src/k8s.io/client-go/plugin/pkg/client/auth/azure:all-srcs",
"//staging/src/k8s.io/client-go/plugin/pkg/client/auth/azure:all-srcs",
"//staging/src/k8s.io/client-go/plugin/pkg/client/auth/gcp:all-srcs",
"//staging/src/k8s.io/client-go/plugin/pkg/client/auth/gcp:all-srcs",
"//staging/src/k8s.io/client-go/plugin/pkg/client/auth/oidc:all-srcs",
"//staging/src/k8s.io/client-go/plugin/pkg/client/auth/oidc:all-srcs",
"//staging/src/k8s.io/client-go/plugin/pkg/client/auth/openstack:all-srcs",
],
],
tags = ["automanaged"],
tags = ["automanaged"],
)
)
staging/src/k8s.io/client-go/plugin/pkg/client/auth/openstack/BUILD
0 → 100644
View file @
a0cebcb5
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_test(
name = "go_default_test",
srcs = ["openstack_test.go"],
library = ":go_default_library",
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = ["openstack.go"],
tags = ["automanaged"],
deps = [
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack:go_default_library",
"//vendor/k8s.io/client-go/rest:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
staging/src/k8s.io/client-go/plugin/pkg/client/auth/openstack/openstack.go
0 → 100644
View file @
a0cebcb5
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
openstack
import
(
"fmt"
"net/http"
"sync"
"time"
"github.com/golang/glog"
"github.com/gophercloud/gophercloud/openstack"
restclient
"k8s.io/client-go/rest"
)
func
init
()
{
if
err
:=
restclient
.
RegisterAuthProviderPlugin
(
"openstack"
,
newOpenstackAuthProvider
);
err
!=
nil
{
glog
.
Fatalf
(
"Failed to register openstack auth plugin: %s"
,
err
)
}
}
// DefaultTTLDuration is the time before a token gets expired.
const
DefaultTTLDuration
=
10
*
time
.
Minute
// openstackAuthProvider is an authprovider for openstack. this provider reads
// the environment variables to determine the client identity, and generates a
// token which will be inserted into the request header later.
type
openstackAuthProvider
struct
{
ttl
time
.
Duration
tokenGetter
TokenGetter
}
// TokenGetter returns a bearer token that can be inserted into request.
type
TokenGetter
interface
{
Token
()
(
string
,
error
)
}
type
tokenGetter
struct
{}
// Token creates a token by authenticate with keystone.
func
(
*
tokenGetter
)
Token
()
(
string
,
error
)
{
options
,
err
:=
openstack
.
AuthOptionsFromEnv
()
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"failed to read openstack env vars: %s"
,
err
)
}
client
,
err
:=
openstack
.
AuthenticatedClient
(
options
)
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"authentication failed: %s"
,
err
)
}
return
client
.
TokenID
,
nil
}
// cachedGetter caches a token until it gets expired, after the expiration, it will
// generate another token and cache it.
type
cachedGetter
struct
{
mutex
sync
.
Mutex
tokenGetter
TokenGetter
token
string
born
time
.
Time
ttl
time
.
Duration
}
// Token returns the current available token, create a new one if expired.
func
(
c
*
cachedGetter
)
Token
()
(
string
,
error
)
{
c
.
mutex
.
Lock
()
defer
c
.
mutex
.
Unlock
()
var
err
error
// no token or exceeds the TTL
if
c
.
token
==
""
||
time
.
Now
()
.
Sub
(
c
.
born
)
>
c
.
ttl
{
c
.
token
,
err
=
c
.
tokenGetter
.
Token
()
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"failed to get token: %s"
,
err
)
}
c
.
born
=
time
.
Now
()
}
return
c
.
token
,
nil
}
// tokenRoundTripper implements the RoundTripper interface: adding the bearer token
// into the request header.
type
tokenRoundTripper
struct
{
http
.
RoundTripper
tokenGetter
TokenGetter
}
// RoundTrip adds the bearer token into the request.
func
(
t
*
tokenRoundTripper
)
RoundTrip
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
// if the authorization header already present, use it.
if
req
.
Header
.
Get
(
"Authorization"
)
!=
""
{
return
t
.
RoundTripper
.
RoundTrip
(
req
)
}
token
,
err
:=
t
.
tokenGetter
.
Token
()
if
err
==
nil
{
req
.
Header
.
Set
(
"Authorization"
,
"Bearer "
+
token
)
}
else
{
glog
.
V
(
4
)
.
Infof
(
"failed to get token: %s"
,
err
)
}
return
t
.
RoundTripper
.
RoundTrip
(
req
)
}
// newOpenstackAuthProvider creates an auth provider which works with openstack
// environment.
func
newOpenstackAuthProvider
(
clusterAddress
string
,
config
map
[
string
]
string
,
persister
restclient
.
AuthProviderConfigPersister
)
(
restclient
.
AuthProvider
,
error
)
{
var
ttlDuration
time
.
Duration
var
err
error
ttl
,
found
:=
config
[
"ttl"
]
if
!
found
{
ttlDuration
=
DefaultTTLDuration
// persist to config
config
[
"ttl"
]
=
ttlDuration
.
String
()
if
err
=
persister
.
Persist
(
config
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to persist config: %s"
,
err
)
}
}
else
{
ttlDuration
,
err
=
time
.
ParseDuration
(
ttl
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to parse ttl config: %s"
,
err
)
}
}
// TODO: read/persist client configuration(OS_XXX env vars) in config
return
&
openstackAuthProvider
{
ttl
:
ttlDuration
,
tokenGetter
:
&
tokenGetter
{},
},
nil
}
func
(
oap
*
openstackAuthProvider
)
WrapTransport
(
rt
http
.
RoundTripper
)
http
.
RoundTripper
{
return
&
tokenRoundTripper
{
RoundTripper
:
rt
,
tokenGetter
:
&
cachedGetter
{
tokenGetter
:
oap
.
tokenGetter
,
ttl
:
oap
.
ttl
,
},
}
}
func
(
oap
*
openstackAuthProvider
)
Login
()
error
{
return
nil
}
staging/src/k8s.io/client-go/plugin/pkg/client/auth/openstack/openstack_test.go
0 → 100644
View file @
a0cebcb5
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
openstack
import
(
"math/rand"
"net/http"
"testing"
"time"
)
// testTokenGetter is a simple random token getter.
type
testTokenGetter
struct
{}
const
LetterBytes
=
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func
RandStringBytes
(
n
int
)
string
{
b
:=
make
([]
byte
,
n
)
for
i
:=
range
b
{
b
[
i
]
=
LetterBytes
[
rand
.
Intn
(
len
(
LetterBytes
))]
}
return
string
(
b
)
}
func
(
*
testTokenGetter
)
Token
()
(
string
,
error
)
{
return
RandStringBytes
(
32
),
nil
}
// testRoundTripper is mocked roundtripper which responds with unauthorized when
// there is no authorization header, otherwise returns status ok.
type
testRoundTripper
struct
{}
func
(
trt
*
testRoundTripper
)
RoundTrip
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
authHeader
:=
req
.
Header
.
Get
(
"Authorization"
)
if
authHeader
==
""
||
authHeader
==
"Bearer "
{
return
&
http
.
Response
{
StatusCode
:
http
.
StatusUnauthorized
,
},
nil
}
return
&
http
.
Response
{
StatusCode
:
http
.
StatusOK
},
nil
}
func
TestOpenstackAuthProvider
(
t
*
testing
.
T
)
{
trt
:=
&
tokenRoundTripper
{
RoundTripper
:
&
testRoundTripper
{},
}
tests
:=
[]
struct
{
name
string
ttl
time
.
Duration
interval
time
.
Duration
same
bool
}{
{
name
:
"normal"
,
ttl
:
2
*
time
.
Second
,
interval
:
1
*
time
.
Second
,
same
:
true
,
},
{
name
:
"expire"
,
ttl
:
1
*
time
.
Second
,
interval
:
2
*
time
.
Second
,
same
:
false
,
},
}
for
_
,
test
:=
range
tests
{
trt
.
tokenGetter
=
&
cachedGetter
{
tokenGetter
:
&
testTokenGetter
{},
ttl
:
test
.
ttl
,
}
req
,
err
:=
http
.
NewRequest
(
http
.
MethodPost
,
"https://test-api-server.com"
,
nil
)
if
err
!=
nil
{
t
.
Errorf
(
"failed to new request: %s"
,
err
)
}
trt
.
RoundTrip
(
req
)
header
:=
req
.
Header
.
Get
(
"Authorization"
)
if
header
==
""
{
t
.
Errorf
(
"expect to see token in header, but is absent"
)
}
time
.
Sleep
(
test
.
interval
)
req
,
err
=
http
.
NewRequest
(
http
.
MethodPost
,
"https://test-api-server.com"
,
nil
)
if
err
!=
nil
{
t
.
Errorf
(
"failed to new request: %s"
,
err
)
}
trt
.
RoundTrip
(
req
)
newHeader
:=
req
.
Header
.
Get
(
"Authorization"
)
if
newHeader
==
""
{
t
.
Errorf
(
"expect to see token in header, but is absent"
)
}
same
:=
newHeader
==
header
if
same
!=
test
.
same
{
t
.
Errorf
(
"expect to get %t when compare header, but saw %t"
,
test
.
same
,
same
)
}
}
}
staging/src/k8s.io/client-go/plugin/pkg/client/auth/plugins.go
View file @
a0cebcb5
...
@@ -21,4 +21,5 @@ import (
...
@@ -21,4 +21,5 @@ import (
_
"k8s.io/client-go/plugin/pkg/client/auth/azure"
_
"k8s.io/client-go/plugin/pkg/client/auth/azure"
_
"k8s.io/client-go/plugin/pkg/client/auth/gcp"
_
"k8s.io/client-go/plugin/pkg/client/auth/gcp"
_
"k8s.io/client-go/plugin/pkg/client/auth/oidc"
_
"k8s.io/client-go/plugin/pkg/client/auth/oidc"
_
"k8s.io/client-go/plugin/pkg/client/auth/openstack"
)
)
Write
Preview
Markdown
is supported
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