Skip to content

Commit

Permalink
Support GCE TPM verification
Browse files Browse the repository at this point in the history
  • Loading branch information
justinsb committed Oct 6, 2021
1 parent a0099ed commit 4dc2c06
Show file tree
Hide file tree
Showing 25 changed files with 836 additions and 14 deletions.
1 change: 1 addition & 0 deletions cmd/kops-controller/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions cmd/kops-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
nodeidentitygce "k8s.io/kops/pkg/nodeidentity/gce"
nodeidentityos "k8s.io/kops/pkg/nodeidentity/openstack"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/gce/tpm/gcetpmverifier"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -94,6 +95,12 @@ func main() {
setupLog.Error(err, "unable to create verifier")
os.Exit(1)
}
} else if opt.Server.Provider.GCE != nil {
verifier, err = gcetpmverifier.NewTPMVerifier(opt.Server.Provider.GCE)
if err != nil {
setupLog.Error(err, "unable to create verifier")
os.Exit(1)
}
} else {
klog.Fatalf("server cloud provider config not provided")
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/kops-controller/pkg/config/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions cmd/kops-controller/pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ limitations under the License.

package config

import "k8s.io/kops/upup/pkg/fi/cloudup/awsup"
import (
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
gcetpm "k8s.io/kops/upup/pkg/fi/cloudup/gce/tpm"
)

type Options struct {
Cloud string `json:"cloud,omitempty"`
Expand Down Expand Up @@ -52,5 +55,6 @@ type ServerOptions struct {
}

type ServerProviderOptions struct {
AWS *awsup.AWSVerifierOptions `json:"aws,omitempty"`
AWS *awsup.AWSVerifierOptions `json:"aws,omitempty"`
GCE *gcetpm.TPMVerifierOptions `json:"gce,omitempty"`
}
7 changes: 4 additions & 3 deletions cmd/kops-controller/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ func (s *Server) bootstrap(w http.ResponseWriter, r *http.Request) {
return
}

id, err := s.verifier.VerifyToken(r.Header.Get("Authorization"), body)
ctx := r.Context()

id, err := s.verifier.VerifyToken(ctx, r.Header.Get("Authorization"), body)
if err != nil {
klog.Infof("bootstrap %s verify err: %v", r.RemoteAddr, err)
w.WriteHeader(http.StatusForbidden)
Expand All @@ -115,8 +117,7 @@ func (s *Server) bootstrap(w http.ResponseWriter, r *http.Request) {
}

req := &nodeup.BootstrapRequest{}
err = json.Unmarshal(body, req)
if err != nil {
if err := json.Unmarshal(body, req); err != nil {
klog.Infof("bootstrap %s decode err: %v", r.RemoteAddr, err)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(fmt.Sprintf("failed to decode: %v", err)))
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/google/go-cmp v0.5.6
github.com/google/go-containerregistry v0.5.1
github.com/google/go-tpm v0.3.2
github.com/google/go-tpm-tools v0.3.0-beta1
github.com/google/uuid v1.2.0
github.com/gophercloud/gophercloud v0.18.0
github.com/gorilla/mux v1.8.0 // indirect
Expand Down
100 changes: 99 additions & 1 deletion go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions nodeup/pkg/model/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions nodeup/pkg/model/bootstrap_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/kops/pkg/wellknownports"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/gce/tpm/gcetpmsigner"
"k8s.io/kops/upup/pkg/fi/nodeup/nodetasks"
)

Expand All @@ -45,6 +46,8 @@ func (b BootstrapClientBuilder) Build(c *fi.ModelBuilderContext) error {
switch kops.CloudProviderID(b.Cluster.Spec.CloudProvider) {
case kops.CloudProviderAWS:
authenticator, err = awsup.NewAWSAuthenticator(b.Cloud.Region())
case kops.CloudProviderGCE:
authenticator, err = gcetpmsigner.NewTPMAuthenticator()
default:
return fmt.Errorf("unsupported cloud provider %s", b.Cluster.Spec.CloudProvider)
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/apis/kops/model/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ import (

// UseKopsControllerForNodeBootstrap is true if nodeup should use kops-controller for bootstrapping.
func UseKopsControllerForNodeBootstrap(cluster *kops.Cluster) bool {
return kops.CloudProviderID(cluster.Spec.CloudProvider) == kops.CloudProviderAWS && cluster.IsKubernetesGTE("1.19")
switch kops.CloudProviderID(cluster.Spec.CloudProvider) {
case kops.CloudProviderAWS:
return cluster.IsKubernetesGTE("1.19")
case kops.CloudProviderGCE:
return cluster.IsKubernetesGTE("1.22")
default:
return false
}
}

// UseCiliumEtcd is true if we are using the Cilium etcd cluster.
Expand Down
8 changes: 6 additions & 2 deletions pkg/bootstrap/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ limitations under the License.

package bootstrap

import (
"context"
)

// Authenticator generates authentication credentials for requests.
type Authenticator interface {
CreateToken(body []byte) (string, error)
Expand All @@ -29,11 +33,11 @@ type VerifyResult struct {
// InstanceGroupName is the name of the kops InstanceGroup this node is a member of.
InstanceGroupName string

// CertificateNames is the names the node is authorized to use for certificates.
// CertificateNames is the alternate names the node is authorized to use for certificates.
CertificateNames []string
}

// Verifier verifies authentication credentials for requests.
type Verifier interface {
VerifyToken(token string, body []byte) (*VerifyResult, error)
VerifyToken(ctx context.Context, token string, body []byte) (*VerifyResult, error)
}
Loading

0 comments on commit 4dc2c06

Please sign in to comment.