Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TLS and CA Certificate support for wins #111

Merged
merged 5 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,25 @@ csi-proxy:
kubeletPath: c:/etc/kubelet.exe
```

#### Enabling Certificate Support for Wins

Wins now supports consuming a certificate when it is required for pulling the CSI proxy tarball from a Rancher Server.
Common situations where this is required are airgapped Rancher environments and self-signed Rancher installations.

```yml
tls-config:
insecure: <true/false>
certFilePath: <path to local certificate>
```

Example:

```yml
tls-config:
insecure: false
certFilePath: c:/etc/rancher/agent/ranchercert
```

## Build

``` powershell
Expand Down
5 changes: 5 additions & 0 deletions cmd/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/rancher/system-agent/pkg/config"
"github.com/rancher/wins/pkg/csiproxy"
"github.com/rancher/wins/pkg/defaults"
"github.com/rancher/wins/pkg/tls"
)

func DefaultConfig() *Config {
Expand All @@ -24,6 +25,9 @@ func DefaultConfig() *Config {
Mode: "watching",
WatchingPath: defaults.UpgradeWatchingPath,
},
TLSConfig: &tls.TLSConfig{
CertFilePath: defaults.CertPath,
},
}
}

Expand All @@ -35,6 +39,7 @@ type Config struct {
Upgrade UpgradeConfig `yaml:"upgrade" json:"upgrade"`
SystemAgent *config.AgentConfig `yaml:"systemagent" json:"systemagent"`
CSIProxy *csiproxy.Config `yaml:"csi-proxy" json:"csi-proxy"`
TLSConfig *tls.TLSConfig `yaml:"tls-config" json:"tls-config"`
}

func (c *Config) Validate() error {
Expand Down
9 changes: 9 additions & 0 deletions pkg/csiproxy/csi.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"strings"

"github.com/pkg/errors"
winsConfig "github.com/rancher/wins/cmd/server/config"
"github.com/rancher/wins/pkg/concierge"
"github.com/rancher/wins/pkg/tls"
)

const (
Expand Down Expand Up @@ -89,6 +91,13 @@ func (p *Proxy) Enable() error {
return err
}
if !ok {
wc := winsConfig.Config{}
if wc.TLSConfig.CertFilePath != "" {
_, err := tls.SetupGenericTLSConfigFromFile(*wc.TLSConfig)
if err != nil {
return err
}
}
if err := p.download(); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/defaults/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ var (
AppCommit = "0000000"
ConfigPath = filepath.Join("c:/", "etc", "rancher", "wins", "config")
UpgradeWatchingPath = filepath.Join("c:/", "etc", "rancher", "wins", "wins.exe")
CertPath = filepath.Join("c:/", "etc", "rancher", "agent", "ranchercert")
)
48 changes: 48 additions & 0 deletions pkg/tls/tls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tls

import (
"crypto/x509"
"fmt"
"io/ioutil"
"os"

"github.com/sirupsen/logrus"
)

type TLSConfig struct {
Insecure *bool `yaml:"insecure" json:"insecure"`
CertFilePath string `yaml:"CertFilePath" json:"CertFilePath"`
}

// SetupGenericTLSConfigFromFile returns a x509 system certificate pool containing the specified certificate file
func SetupGenericTLSConfigFromFile(config TLSConfig) (*x509.CertPool, error) {
if config.CertFilePath == "" {
logrus.Info("[SetupGenericTLSConfigFromFile] specified certificate file path is empty, not modifying system certificate store")
return nil, nil
}

// Get the System certificate store, continue with a new/empty pool on error
systemCerts, err := x509.SystemCertPool()
if err != nil || systemCerts == nil {
logrus.Warnf("[SetupGenericTLSConfigFromFile] failed to return System Cert Pool, creating new one: %v", err)
systemCerts = x509.NewCertPool()
}

localCertPath, err := os.Stat(config.CertFilePath)
if err != nil {
return nil, fmt.Errorf("[SetupGenericTLSConfigFromFile] unable to read certificate %s from %s: %v", localCertPath.Name(), config.CertFilePath, err)
}

certs, err := ioutil.ReadFile(config.CertFilePath)
if err != nil {
return nil, fmt.Errorf("[SetupGenericTLSConfigFromFile] failed to read local cert file %q: %v", config.CertFilePath, err)
}

// Append the specified cert to the system pool
if ok := systemCerts.AppendCertsFromPEM(certs); !ok {
return nil, fmt.Errorf("[SetupGenericTLSConfigFromFile] unable to append cert %s to store, using system certs only", config.CertFilePath)
}

logrus.Infof("[SetupGenericTLSConfigFromFile] successfully loaded %s certificate into system cert store", config.CertFilePath)
return systemCerts, nil
}