Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
617e969a
K
kubesphere
项目概览
水淹萌龙
/
kubesphere
与 Fork 源项目一致
Fork自
KubeSphere / kubesphere
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kubesphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
617e969a
编写于
11月 13, 2020
作者:
H
hongming
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: private key not match public key
Signed-off-by:
N
hongming
<
talonwan@yunify.com
>
上级
1cb1eb4f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
48 addition
and
69 deletion
+48
-69
pkg/controller/certificatesigningrequest/certificatesigningrequest_controller.go
...atesigningrequest/certificatesigningrequest_controller.go
+2
-5
pkg/models/kubeconfig/kubeconfig.go
pkg/models/kubeconfig/kubeconfig.go
+46
-64
未找到文件。
pkg/controller/certificatesigningrequest/certificatesigningrequest_controller.go
浏览文件 @
617e969a
...
...
@@ -220,6 +220,7 @@ func (c *Controller) reconcile(key string) error {
if
len
(
csr
.
Status
.
Certificate
)
>
0
{
err
=
c
.
UpdateKubeconfig
(
csr
)
if
err
!=
nil
{
// kubeconfig not generated
klog
.
Error
(
err
)
return
err
}
...
...
@@ -258,7 +259,6 @@ func (c *Controller) Approve(csr *certificatesv1beta1.CertificateSigningRequest)
// approve csr
csr
,
err
:=
c
.
k8sclient
.
CertificatesV1beta1
()
.
CertificateSigningRequests
()
.
UpdateApproval
(
csr
)
if
err
!=
nil
{
klog
.
Errorln
(
err
)
return
err
...
...
@@ -269,12 +269,9 @@ func (c *Controller) Approve(csr *certificatesv1beta1.CertificateSigningRequest)
func
(
c
*
Controller
)
UpdateKubeconfig
(
csr
*
certificatesv1beta1
.
CertificateSigningRequest
)
error
{
username
:=
csr
.
Labels
[
constants
.
UsernameLabelKey
]
err
:=
c
.
kubeconfigOperator
.
UpdateKubeconfig
(
username
,
csr
.
Status
.
Certificate
)
err
:=
c
.
kubeconfigOperator
.
UpdateKubeconfig
(
username
,
csr
)
if
err
!=
nil
{
klog
.
Error
(
err
)
}
return
err
}
pkg/models/kubeconfig/kubeconfig.go
浏览文件 @
617e969a
...
...
@@ -44,7 +44,7 @@ import (
)
const
(
inClusterCAFilePath
=
"/run/secrets/kubernetes.io/serviceaccount/ca.crt"
inClusterCAFilePath
=
"/
var/
run/secrets/kubernetes.io/serviceaccount/ca.crt"
configMapPrefix
=
"kubeconfig-"
kubeconfigNameFormat
=
configMapPrefix
+
"%s"
defaultClusterName
=
"local"
...
...
@@ -52,12 +52,13 @@ const (
kubeconfigFileName
=
"config"
configMapKind
=
"ConfigMap"
configMapAPIVersion
=
"v1"
privateKeyAnnotation
=
"kubesphere.io/private-key"
)
type
Interface
interface
{
GetKubeConfig
(
username
string
)
(
string
,
error
)
CreateKubeConfig
(
user
*
iamv1alpha2
.
User
)
error
UpdateKubeconfig
(
username
string
,
c
ertificate
[]
byte
)
error
UpdateKubeconfig
(
username
string
,
c
sr
*
certificatesv1beta1
.
CertificateSigningRequest
)
error
}
type
operator
struct
{
...
...
@@ -76,11 +77,8 @@ func NewReadOnlyOperator(configMapInformer corev1informers.ConfigMapInformer, ma
}
func
(
o
*
operator
)
CreateKubeConfig
(
user
*
iamv1alpha2
.
User
)
error
{
configName
:=
fmt
.
Sprintf
(
kubeconfigNameFormat
,
user
.
Name
)
_
,
err
:=
o
.
configMapInformer
.
Lister
()
.
ConfigMaps
(
constants
.
KubeSphereControlNamespace
)
.
Get
(
configName
)
// already exist
if
err
==
nil
{
return
nil
...
...
@@ -104,15 +102,12 @@ func (o *operator) CreateKubeConfig(user *iamv1alpha2.User) error {
}
}
clientKey
,
err
:=
o
.
createCSR
(
user
.
Name
)
if
err
!=
nil
{
if
err
=
o
.
createCSR
(
user
.
Name
);
err
!=
nil
{
klog
.
Errorln
(
err
)
return
err
}
currentContext
:=
fmt
.
Sprintf
(
"%s@%s"
,
user
.
Name
,
defaultClusterName
)
config
:=
clientcmdapi
.
Config
{
Kind
:
configMapKind
,
APIVersion
:
configMapAPIVersion
,
...
...
@@ -122,9 +117,6 @@ func (o *operator) CreateKubeConfig(user *iamv1alpha2.User) error {
InsecureSkipTLSVerify
:
false
,
CertificateAuthorityData
:
ca
,
}},
AuthInfos
:
map
[
string
]
*
clientcmdapi
.
AuthInfo
{
user
.
Name
:
{
ClientKeyData
:
clientKey
,
}},
Contexts
:
map
[
string
]
*
clientcmdapi
.
Context
{
currentContext
:
{
Cluster
:
defaultClusterName
,
AuthInfo
:
user
.
Name
,
...
...
@@ -134,26 +126,29 @@ func (o *operator) CreateKubeConfig(user *iamv1alpha2.User) error {
}
kubeconfig
,
err
:=
clientcmd
.
Write
(
config
)
if
err
!=
nil
{
klog
.
Error
(
err
)
return
err
}
cm
:=
&
corev1
.
ConfigMap
{
TypeMeta
:
metav1
.
TypeMeta
{
Kind
:
configMapKind
,
APIVersion
:
configMapAPIVersion
},
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
configName
,
Labels
:
map
[
string
]
string
{
constants
.
UsernameLabelKey
:
user
.
Name
}},
Data
:
map
[
string
]
string
{
kubeconfigFileName
:
string
(
kubeconfig
)}}
err
=
controllerutil
.
SetControllerReference
(
user
,
cm
,
scheme
.
Scheme
)
cm
:=
&
corev1
.
ConfigMap
{
TypeMeta
:
metav1
.
TypeMeta
{
Kind
:
configMapKind
,
APIVersion
:
configMapAPIVersion
,
},
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
configName
,
Labels
:
map
[
string
]
string
{
constants
.
UsernameLabelKey
:
user
.
Name
},
},
Data
:
map
[
string
]
string
{
kubeconfigFileName
:
string
(
kubeconfig
)},
}
if
err
!=
nil
{
if
err
=
controllerutil
.
SetControllerReference
(
user
,
cm
,
scheme
.
Scheme
);
err
!=
nil
{
klog
.
Errorln
(
err
)
return
err
}
_
,
err
=
o
.
k8sClient
.
CoreV1
()
.
ConfigMaps
(
constants
.
KubeSphereControlNamespace
)
.
Create
(
cm
)
if
err
!=
nil
{
if
_
,
err
=
o
.
k8sClient
.
CoreV1
()
.
ConfigMaps
(
constants
.
KubeSphereControlNamespace
)
.
Create
(
cm
);
err
!=
nil
{
klog
.
Errorln
(
err
)
return
err
}
...
...
@@ -170,23 +165,19 @@ func (o *operator) GetKubeConfig(username string) (string, error) {
}
data
:=
[]
byte
(
configMap
.
Data
[
kubeconfigFileName
])
kubeconfig
,
err
:=
clientcmd
.
Load
(
data
)
if
err
!=
nil
{
klog
.
Errorln
(
err
)
return
""
,
err
}
masterURL
:=
o
.
masterURL
// server host override
if
cluster
:=
kubeconfig
.
Clusters
[
defaultClusterName
];
cluster
!=
nil
&&
masterURL
!=
""
{
cluster
.
Server
=
masterURL
}
data
,
err
=
clientcmd
.
Write
(
*
kubeconfig
)
if
err
!=
nil
{
klog
.
Errorln
(
err
)
return
""
,
err
...
...
@@ -195,55 +186,49 @@ func (o *operator) GetKubeConfig(username string) (string, error) {
return
string
(
data
),
nil
}
func
(
o
*
operator
)
createCSR
(
username
string
)
([]
byte
,
error
)
{
func
(
o
*
operator
)
createCSR
(
username
string
)
error
{
csrConfig
:=
&
certutil
.
Config
{
CommonName
:
username
,
Organization
:
nil
,
AltNames
:
certutil
.
AltNames
{},
Usages
:
[]
x509
.
ExtKeyUsage
{
x509
.
ExtKeyUsageClientAuth
},
}
x509csr
,
x509key
,
err
:=
pkiutil
.
NewCSRAndKey
(
csrConfig
)
if
err
!=
nil
{
klog
.
Errorln
(
err
)
return
nil
,
err
return
err
}
var
csrBuffer
,
keyBuffer
bytes
.
Buffer
err
=
pem
.
Encode
(
&
keyBuffer
,
&
pem
.
Block
{
Type
:
"PRIVATE KEY"
,
Bytes
:
x509
.
MarshalPKCS1PrivateKey
(
x509key
)})
if
err
!=
nil
{
if
err
=
pem
.
Encode
(
&
keyBuffer
,
&
pem
.
Block
{
Type
:
"PRIVATE KEY"
,
Bytes
:
x509
.
MarshalPKCS1PrivateKey
(
x509key
)});
err
!=
nil
{
klog
.
Errorln
(
err
)
return
nil
,
err
return
err
}
csrBytes
,
err
:=
x509
.
CreateCertificateRequest
(
rand
.
Reader
,
x509csr
,
x509key
)
if
err
!=
nil
{
var
csrBytes
[]
byte
if
csrBytes
,
err
=
x509
.
CreateCertificateRequest
(
rand
.
Reader
,
x509csr
,
x509key
);
err
!=
nil
{
klog
.
Errorln
(
err
)
return
nil
,
err
return
err
}
err
=
pem
.
Encode
(
&
csrBuffer
,
&
pem
.
Block
{
Type
:
"CERTIFICATE REQUEST"
,
Bytes
:
csrBytes
})
if
err
!=
nil
{
if
err
=
pem
.
Encode
(
&
csrBuffer
,
&
pem
.
Block
{
Type
:
"CERTIFICATE REQUEST"
,
Bytes
:
csrBytes
});
err
!=
nil
{
klog
.
Errorln
(
err
)
return
nil
,
err
return
err
}
csr
:=
csrBuffer
.
Bytes
()
key
:=
keyBuffer
.
Bytes
()
csrName
:=
fmt
.
Sprintf
(
"%s-csr-%d"
,
username
,
time
.
Now
()
.
Unix
())
k8sCSR
:=
&
certificatesv1beta1
.
CertificateSigningRequest
{
TypeMeta
:
metav1
.
TypeMeta
{
Kind
:
"CertificateSigningRequest"
,
APIVersion
:
"certificates.k8s.io/v1beta1"
,
},
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
csrName
,
Labels
:
map
[
string
]
string
{
constants
.
UsernameLabelKey
:
username
},
Name
:
csrName
,
Labels
:
map
[
string
]
string
{
constants
.
UsernameLabelKey
:
username
},
Annotations
:
map
[
string
]
string
{
privateKeyAnnotation
:
string
(
key
)},
},
Spec
:
certificatesv1beta1
.
CertificateSigningRequestSpec
{
Request
:
csr
,
...
...
@@ -254,17 +239,16 @@ func (o *operator) createCSR(username string) ([]byte, error) {
}
// create csr
k8sCSR
,
err
=
o
.
k8sClient
.
CertificatesV1beta1
()
.
CertificateSigningRequests
()
.
Create
(
k8sCSR
)
if
err
!=
nil
{
if
_
,
err
=
o
.
k8sClient
.
CertificatesV1beta1
()
.
CertificateSigningRequests
()
.
Create
(
k8sCSR
);
err
!=
nil
{
klog
.
Errorln
(
err
)
return
nil
,
err
return
err
}
return
key
,
nil
return
nil
}
func
(
o
*
operator
)
UpdateKubeconfig
(
username
string
,
certificate
[]
byte
)
error
{
// Update client key and client certificate after CertificateSigningRequest has been approved
func
(
o
*
operator
)
UpdateKubeconfig
(
username
string
,
csr
*
certificatesv1beta1
.
CertificateSigningRequest
)
error
{
configName
:=
fmt
.
Sprintf
(
kubeconfigNameFormat
,
username
)
configMap
,
err
:=
o
.
k8sClient
.
CoreV1
()
.
ConfigMaps
(
constants
.
KubeSphereControlNamespace
)
.
Get
(
configName
,
metav1
.
GetOptions
{})
if
err
!=
nil
{
...
...
@@ -272,7 +256,7 @@ func (o *operator) UpdateKubeconfig(username string, certificate []byte) error {
return
err
}
configMap
=
app
endCert
(
configMap
,
certificate
)
configMap
=
app
lyCert
(
configMap
,
csr
)
_
,
err
=
o
.
k8sClient
.
CoreV1
()
.
ConfigMaps
(
constants
.
KubeSphereControlNamespace
)
.
Update
(
configMap
)
if
err
!=
nil
{
klog
.
Errorln
(
err
)
...
...
@@ -281,33 +265,31 @@ func (o *operator) UpdateKubeconfig(username string, certificate []byte) error {
return
nil
}
func
app
endCert
(
cm
*
corev1
.
ConfigMap
,
cert
[]
byte
)
*
corev1
.
ConfigMap
{
func
app
lyCert
(
cm
*
corev1
.
ConfigMap
,
csr
*
certificatesv1beta1
.
CertificateSigningRequest
)
*
corev1
.
ConfigMap
{
data
:=
[]
byte
(
cm
.
Data
[
kubeconfigFileName
])
kubeconfig
,
err
:=
clientcmd
.
Load
(
data
)
// ignore if invalid format
if
err
!=
nil
{
klog
.
Warning
(
err
)
klog
.
Error
(
err
)
return
cm
}
username
:=
getControlledUsername
(
cm
)
if
kubeconfig
.
AuthInfos
[
username
]
!=
nil
{
kubeconfig
.
AuthInfos
[
username
]
.
ClientCertificateData
=
cert
privateKey
:=
csr
.
Annotations
[
privateKeyAnnotation
]
clientCert
:=
csr
.
Status
.
Certificate
kubeconfig
.
AuthInfos
=
map
[
string
]
*
clientcmdapi
.
AuthInfo
{
username
:
{
ClientKeyData
:
[]
byte
(
privateKey
),
ClientCertificateData
:
clientCert
,
},
}
data
,
err
=
clientcmd
.
Write
(
*
kubeconfig
)
// ignore if invalid format
if
err
!=
nil
{
klog
.
Warning
(
err
)
klog
.
Error
(
err
)
return
cm
}
cm
.
Data
[
kubeconfigFileName
]
=
string
(
data
)
return
cm
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录