-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add util to self sign cert and update to WebhookConfiguration
- Loading branch information
Showing
10 changed files
with
1,189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/** | ||
* Copyright 2024 The KusionStack 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 | ||
* | ||
* https://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 cert | ||
|
||
import ( | ||
"context" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"github.com/zoumo/golib/cert" | ||
certutil "github.com/zoumo/golib/cert" | ||
) | ||
|
||
type ( | ||
Config = cert.Config | ||
AltNames = cert.AltNames | ||
) | ||
|
||
type ServingCerts struct { | ||
Key []byte | ||
Cert []byte | ||
CAKey []byte | ||
CACert []byte | ||
} | ||
|
||
func (c *ServingCerts) Validate(host string) error { | ||
if len(c.Key) == 0 { | ||
return fmt.Errorf("private key is empty") | ||
} | ||
if len(c.Cert) == 0 { | ||
return fmt.Errorf("cetificate is empty") | ||
} | ||
if len(c.CAKey) == 0 { | ||
return fmt.Errorf("CA private key is empty") | ||
} | ||
if len(c.CACert) == 0 { | ||
return fmt.Errorf("CA certificate is empty") | ||
} | ||
|
||
tlsCert, err := cert.X509KeyPair(c.Cert, c.Key) | ||
if err != nil { | ||
return fmt.Errorf("invalid x509 keypair: %w", err) | ||
} | ||
|
||
// verify cert with ca and host | ||
pool := x509.NewCertPool() | ||
if !pool.AppendCertsFromPEM(c.CACert) { | ||
return fmt.Errorf("no valid CA certificate found") | ||
} | ||
|
||
options := x509.VerifyOptions{ | ||
Roots: pool, | ||
DNSName: host, | ||
CurrentTime: time.Now(), | ||
} | ||
|
||
_, err = tlsCert.X509Cert.Verify(options) | ||
return err | ||
} | ||
|
||
func GenerateSelfSignedCerts(cfg Config) (*ServingCerts, error) { | ||
caKey, caCert, key, cert, err := generateSelfSignedCertKey(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
keyPEM := certutil.MarshalRSAPrivateKeyToPEM(key) | ||
cerPEM := certutil.MarshalCertToPEM(cert) | ||
caKeyPEM := certutil.MarshalRSAPrivateKeyToPEM(caKey) | ||
caCertPEM := certutil.MarshalCertToPEM(caCert) | ||
|
||
return &ServingCerts{ | ||
CAKey: caKeyPEM.EncodeToMemory(), | ||
CACert: caCertPEM.EncodeToMemory(), | ||
Key: keyPEM.EncodeToMemory(), | ||
Cert: cerPEM.EncodeToMemory(), | ||
}, nil | ||
} | ||
|
||
func GenerateSelfSignedCertKeyIfNotExist(path string, cfg cert.Config) error { | ||
fscerts, err := NewFSProvider(path, FSOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
return fscerts.Ensure(context.Background(), cfg) | ||
} | ||
|
||
func generateSelfSignedCertKey(cfg Config) (*rsa.PrivateKey, *x509.Certificate, *rsa.PrivateKey, *x509.Certificate, error) { | ||
caKey, err := certutil.NewRSAPrivateKey() | ||
if err != nil { | ||
return nil, nil, nil, nil, err | ||
} | ||
|
||
caCert, err := certutil.NewSelfSignedCACert(certutil.Config{ | ||
CommonName: fmt.Sprintf("%s-ca@%d", cfg.CommonName, time.Now().Unix()), | ||
}, caKey) | ||
if err != nil { | ||
return nil, nil, nil, nil, err | ||
} | ||
|
||
key, err := certutil.NewRSAPrivateKey() | ||
if err != nil { | ||
return nil, nil, nil, nil, err | ||
} | ||
|
||
if ip := net.ParseIP(cfg.CommonName); ip != nil { | ||
cfg.AltNames.IPs = append(cfg.AltNames.IPs, ip) | ||
} else { | ||
cfg.AltNames.DNSNames = append(cfg.AltNames.DNSNames, cfg.CommonName) | ||
} | ||
|
||
cert, err := certutil.NewSignedCert(cfg, key, caKey, caCert) | ||
if err != nil { | ||
return nil, nil, nil, nil, err | ||
} | ||
return caKey, caCert, key, cert, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright 2024 The KusionStack 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 | ||
* | ||
* https://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 cert | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/zoumo/golib/cert" | ||
) | ||
|
||
func TestServingCerts_Validate(t *testing.T) { | ||
cfg := Config{ | ||
CommonName: "foo.example.com", | ||
AltNames: cert.AltNames{ | ||
DNSNames: []string{"bar.example.com"}, | ||
}, | ||
} | ||
certs, err := GenerateSelfSignedCerts(cfg) | ||
assert.Nil(t, err) | ||
assert.Nil(t, certs.Validate("foo.example.com")) | ||
assert.Nil(t, certs.Validate("bar.example.com")) | ||
assert.NotNil(t, certs.Validate("unknown.example.com")) | ||
} | ||
|
||
func TestGenerateSelfSignedCerts(t *testing.T) { | ||
cfg := Config{ | ||
CommonName: "rollout.rollout-system.svc", | ||
AltNames: cert.AltNames{ | ||
DNSNames: []string{"rollout.rollout-system.svc", "foo.example.com"}, | ||
}, | ||
} | ||
certs, err := GenerateSelfSignedCerts(cfg) | ||
assert.Nil(t, err) | ||
|
||
err = certs.Validate("rollout.rollout-system.svc") | ||
assert.Nil(t, err) | ||
|
||
err = certs.Validate("foo.example.com") | ||
assert.Nil(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright 2024 The KusionStack 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 | ||
* | ||
* https://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 cert | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
) | ||
|
||
var errNotFound = errors.New("not found") | ||
|
||
func newNotFound(name string, err error) error { | ||
return fmt.Errorf("%s %w: %v", name, errNotFound, err) | ||
} | ||
|
||
func IsNotFound(err error) bool { | ||
return apierrors.IsNotFound(err) || errors.Is(err, errNotFound) | ||
} | ||
|
||
func IsConflict(err error) bool { | ||
return apierrors.IsAlreadyExists(err) || apierrors.IsConflict(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright 2024 The KusionStack 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 | ||
* | ||
* https://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 cert | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
func TestIsNotFound(t *testing.T) { | ||
err := newNotFound("testFile", errors.New("testError")) | ||
assert.True(t, IsNotFound(err)) | ||
|
||
err = apierrors.NewNotFound(schema.GroupResource{}, "testResource") | ||
assert.True(t, IsNotFound(err)) | ||
} |
Oops, something went wrong.