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

Bug 1872369: Fix reading TLSConfig for HelmChartRepository #6347

Merged

Conversation

pedjak
Copy link
Contributor

@pedjak pedjak commented Aug 17, 2020

Secret containing TLS config was wrongly expected to be found in HelmChartRepository.spec.connectionConfig.tlsconfig.name path.

According to https://github.com/openshift/api/blob/master/helm/v1beta1/types_helm.go#L76 TLS client config is to be found in secret whose name is set in HelmChartRepository.spec.connectionConfig.tlsClientConfig.name path

Additional fixes/improvements:

  • Refactored Helm chart repo model for better testability
  • More tests added

@pedjak
Copy link
Contributor Author

pedjak commented Aug 18, 2020

/retest

2 similar comments
@pedjak
Copy link
Contributor Author

pedjak commented Aug 18, 2020

/retest

@pedjak
Copy link
Contributor Author

pedjak commented Aug 25, 2020

/retest

@spadgett
Copy link
Member

/assign @jhadvig

@spadgett
Copy link
Member

Can you add more a better description to the PR so it's clear what the purpose is? This will require a Bugzilla bug to merge since we're past feature freeze.


func (b helmRepoGetter) unmarshallConfig(repo unstructured.Unstructured) (*helmRepo, error) {
h := &helmRepo{}
urlValue, _, err := unstructured.NestedString(repo.Object, "spec", "connectionConfig", "url")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you bump openshift/api, you should have a real struct with these fields, which would be better for type safety.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would love too, and we tried to add openshift/client-go as the dependency (it contains all necessary types), but we cannot make it work currently in console, because client-go depends on k8s 1.19, but Helm (we depend on here) requires k8s 1.18. Tried to force it, but Helm is not yet compatible to k8s 1.19.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any plan to make it compatible ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, when Helm moves to 1.19.

}

func (b helmRepoGetter) secretValue(name string, dataField string) ([]byte, error) {
secret, err := b.CoreClient.Secrets(configNamespace).Get(context.TODO(), name, v1.GetOptions{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the experience for users who don't have authority to get the secret?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They do not have access to that chart repo and consequently to charts managed there. UI will not list them at all.

@pedjak pedjak changed the title Deserialize TLSConfig for correct secret Deserialize TLSConfig from correct secret Aug 25, 2020
@pedjak pedjak changed the title Deserialize TLSConfig from correct secret Fix reading TLSConfig for HelmChartRepository Aug 25, 2020
@pedjak pedjak changed the title Fix reading TLSConfig for HelmChartRepository Bug 1872369: Fix reading TLSConfig for HelmChartRepository Aug 25, 2020
@openshift-ci-robot openshift-ci-robot added bugzilla/severity-urgent Referenced Bugzilla bug's severity is urgent for the branch this PR is targeting. bugzilla/invalid-bug Indicates that a referenced Bugzilla bug is invalid for the branch this PR is targeting. labels Aug 25, 2020
@openshift-ci-robot
Copy link
Contributor

@pedjak: This pull request references Bugzilla bug 1872369, which is invalid:

  • expected the bug to target the "4.6.0" release, but it targets "---" instead

Comment /bugzilla refresh to re-evaluate validity if changes to the Bugzilla bug are made, or edit the title of this pull request to link to a different bug.

In response to this:

Bug 1872369: Fix reading TLSConfig for HelmChartRepository

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@pedjak
Copy link
Contributor Author

pedjak commented Aug 26, 2020

/bugzilla refresh

@openshift-ci-robot openshift-ci-robot added bugzilla/severity-high Referenced Bugzilla bug's severity is high for the branch this PR is targeting. bugzilla/valid-bug Indicates that a referenced Bugzilla bug is valid for the branch this PR is targeting. and removed bugzilla/severity-urgent Referenced Bugzilla bug's severity is urgent for the branch this PR is targeting. labels Aug 26, 2020
@openshift-ci-robot
Copy link
Contributor

@pedjak: This pull request references Bugzilla bug 1872369, which is valid. The bug has been moved to the POST state. The bug has been updated to refer to the pull request using the external bug tracker.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target release (4.6.0) matches configured target release for branch (4.6.0)
  • bug is in the state NEW, which is one of the valid states (NEW, ASSIGNED, ON_DEV, POST, POST)

In response to this:

/bugzilla refresh

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@openshift-ci-robot openshift-ci-robot removed the bugzilla/invalid-bug Indicates that a referenced Bugzilla bug is invalid for the branch this PR is targeting. label Aug 26, 2020
@pedjak
Copy link
Contributor Author

pedjak commented Aug 26, 2020

/retest

Copy link
Member

@jhadvig jhadvig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedjak thanks for the PR. couple of questions and comments after the first round of review.

type helmRepo struct {
Name string
Url *url.URL
TlsClientConfig *tls.Config
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
TlsClientConfig *tls.Config
TLSClientConfig *tls.Config

func (b *helmRepoGetter) List() ([]*helmRepo, error) {
var helmRepos []*helmRepo
repos, err := b.Client.Resource(helmChartRepositoryGVK).List(context.TODO(), v1.ListOptions{})
if err != nil || len(repos.Items) == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is silencing the error here intentional ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, check the comment in the next line.


"github.com/openshift/console/pkg/auth"
"github.com/openshift/console/pkg/helm/actions"
"github.com/openshift/console/pkg/serverutils"
)

var (
plog = capnslog.NewPackageLogger("github.com/openshift/console", "helm")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
plog = capnslog.NewPackageLogger("github.com/openshift/console", "helm")
plog = capnslog.NewPackageLogger("github.com/openshift/console", "helm/handlers")

handle("/api/helm/template", authHandlerWithUser(helmHandlers.HandleHelmRenderManifests))
handle("/api/helm/releases", authHandlerWithUser(helmHandlers.HandleHelmList))
handle("/api/helm/chart", authHandlerWithUser(helmHandlers.HandleChartGet))
handle("/api/helm/release/history", authHandlerWithUser(helmHandlers.HandleGetReleaseHistory))
handle("/api/helm/charts/index.yaml", authHandlerWithUser(helmHandlers.HandleGetRepos))
handle("/api/helm/charts/index.yaml", authHandlerWithUser(helmHandlers.IndexFile))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
handle("/api/helm/charts/index.yaml", authHandlerWithUser(helmHandlers.IndexFile))
handle("/api/helm/charts/index.yaml", authHandlerWithUser(helmHandlers.HandleIndexFile))

t.Errorf("Response status code isn't matching expected %d recieved %d", tt.httpStatusCode, response.Code)
}
if response.Code != tt.httpStatusCode {
t.Errorf("Response status code isn't matching expected %d recieved %d", tt.httpStatusCode, response.Code)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.Errorf("Response status code isn't matching expected %d recieved %d", tt.httpStatusCode, response.Code)
t.Errorf("Response status code isn't matching expected %d received %d", tt.httpStatusCode, response.Code)


func (b helmRepoGetter) unmarshallConfig(repo unstructured.Unstructured) (*helmRepo, error) {
h := &helmRepo{}
urlValue, _, err := unstructured.NestedString(repo.Object, "spec", "connectionConfig", "url")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any plan to make it compatible ?

expectedRepoName: []string{"sample-repo-1", "sample-repo-2"},
},
{
name: "return 1 repos found in cluster",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
name: "return 1 repos found in cluster",
name: "return 1 repo found in cluster",

},
{
name: "return default repo when none are declared in cluster",
ReposNum: 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be 1 since the default one is returned ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it represents the number of HelmChartRepos found in cluster

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, we are testing the situation when there are no HelmChartRepo CRs available in the cluster - in that case, we should still fall back on the default repo.

"github.com/openshift/library-go/pkg/crypto"
)

var (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this needs to be a global variable ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it private to this package. we use it to retrieve the instances. I am open to any better approach.

oscrypto "github.com/openshift/library-go/pkg/crypto"
)

var (
log = capnslog.NewPackageLogger("github.com/openshift/console", "pkg/helm")
log = capnslog.NewPackageLogger("github.com/openshift/console", "pkg/helm")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log = capnslog.NewPackageLogger("github.com/openshift/console", "pkg/helm")
log = capnslog.NewPackageLogger("github.com/openshift/console", "helm/chartproxy")

Copy link
Member

@jhadvig jhadvig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some additional comments to fix. Otherwise looks good.

pkg/helm/chartproxy/config.go Outdated Show resolved Hide resolved
return nil, err
}
}
h.TlSClientConfig = &tls.Config{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
h.TlSClientConfig = &tls.Config{
h.TLSClientConfig = oscrypto.SecureTLSConfig(&tls.Config{

}
h.TlSClientConfig = &tls.Config{
RootCAs: rootCAs,
CipherSuites: crypto.DefaultCiphers(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be removed, oscrypto.SecureTLSConfig() will take care of it.

func (b helmRepoGetter) secretValue(name string, dataField string) ([]byte, error) {
secret, err := b.CoreClient.Secrets(configNamespace).Get(context.TODO(), name, v1.GetOptions{})
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to read secret %s", name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
return nil, errors.New(fmt.Sprintf("Failed to read secret %s", name))
return nil, errors.New(fmt.Sprintf("Failed to GET secret %s", name))

func (b helmRepoGetter) configMapValue(name string, dataField string) ([]byte, error) {
configMap, err := b.CoreClient.ConfigMaps(configNamespace).Get(context.TODO(), name, v1.GetOptions{})
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to read configmap %s", name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
return nil, errors.New(fmt.Sprintf("Failed to read configmap %s", name))
return nil, errors.New(fmt.Sprintf("Failed to GET configmap %s", name))


type helmRepo struct {
Name string
Url *url.URL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Url *url.URL
URL *url.URL

if err != nil {
return nil, err
}
indexUrl := hr.Url.String()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
indexUrl := hr.Url.String()
indexURL := hr.Url.String()

if !strings.HasSuffix(indexUrl, "/index.yaml") {
indexUrl += "/index.yaml"
}
resp, err := httpClient.Get(indexUrl)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resp, err := httpClient.Get(indexUrl)
resp, err := httpClient.Get(indexURL)

srv.HelmChartRepoProxyConfig = &proxy.Config{
DefaultRepo = helmRepo{
Name: "redhat-helm-charts",
Url: repoURL,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Url: repoURL,
URL: repoURL,

@@ -33,14 +32,13 @@ func RegisterFlags(fs *flag.FlagSet) *config {
return cfg
}

func (cfg *config) Configure(srv *server.Server) {
func (cfg *config) Configure() {
repoURL := bridge.ValidateFlagIsURL("helm-chart-repo-repoUrl", cfg.repoUrl)

var rootCAs *x509.CertPool
if cfg.repoCaFile != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if cfg.repoCaFile != "" {
if cfg.repoCAFile != "" {

@@ -33,14 +32,13 @@ func RegisterFlags(fs *flag.FlagSet) *config {
return cfg
}

func (cfg *config) Configure(srv *server.Server) {
func (cfg *config) Configure() {
repoURL := bridge.ValidateFlagIsURL("helm-chart-repo-repoUrl", cfg.repoUrl)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
repoURL := bridge.ValidateFlagIsURL("helm-chart-repo-repoUrl", cfg.repoUrl)
repoURL := bridge.ValidateFlagIsURL("helm-chart-repo-url", cfg.repoURL)

repoURL := bridge.ValidateFlagIsURL("helm-chart-repo-repoUrl", cfg.repoUrl)

var rootCAs *x509.CertPool
if cfg.repoCaFile != "" {
rootCAs = x509.NewCertPool()
certPEM, err := ioutil.ReadFile(cfg.repoCaFile)
srv.HelmDefaultRepoCACert = certPEM
if err != nil {
log.Fatalf("failed to read helm chart repo ca file %v : %v", cfg.repoCaFile, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.Fatalf("failed to read helm chart repo ca file %v : %v", cfg.repoCaFile, err)
log.Fatalf("failed to read helm chart repo ca file %v : %v", cfg.repoCAFile, err)

@@ -33,14 +32,13 @@ func RegisterFlags(fs *flag.FlagSet) *config {
return cfg
}

func (cfg *config) Configure(srv *server.Server) {
func (cfg *config) Configure() {
repoURL := bridge.ValidateFlagIsURL("helm-chart-repo-repoUrl", cfg.repoUrl)

var rootCAs *x509.CertPool
if cfg.repoCaFile != "" {
rootCAs = x509.NewCertPool()
certPEM, err := ioutil.ReadFile(cfg.repoCaFile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
certPEM, err := ioutil.ReadFile(cfg.repoCaFile)
certPEM, err := ioutil.ReadFile(cfg.repoCAFile)

indexFileContennts = append(indexFileContennts, "")
}
client := fake.K8sDynamicClient(indexFileContennts...)
cfg := config{repoUrl: "https://default-url.com"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cfg := config{repoUrl: "https://default-url.com"}
cfg := config{repoURL: "https://default-url.com"}

@pedjak pedjak force-pushed the fix-helm-repo-tlsconf-reading branch from e5a854a to 6b8c619 Compare September 3, 2020 12:14
RootCAs: rootCAs,
})
if tlsReference != "" {
tlsCert, err := b.secretValue(tlsReference, "tls.crt")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

secretValue() method is making the API call. Should be enough to call it once and check the wanted field.

        tlsSecret, err := b.CoreClient.Secrets(configNamespace).Get(context.TODO(), name, v1.GetOptions{})
	if err != nil {
		return nil, errors.New(fmt.Sprintf("Failed to GET secret %s", name))

	}
        tlsCert, err := b.secretValue(tlsSecret, "tls.crt")
        tlsCert, err := b.secretValue(tlsSecret, "tls.key")


var rootCAs *x509.CertPool
if caReference != "" {
caCert, err := b.configMapValue(caReference, "ca-bundle.crt")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would pull the API call for GETting the CM from the configMapValue method and just check for it ... check the comment below

Additional fixes/improvements:

* Refactored Helm chart repo model for better testability
* More tests added

Co-authored-by: Predrag Knezevic <pknezevi@redhat.com>
@pedjak pedjak force-pushed the fix-helm-repo-tlsconf-reading branch from 277bfe0 to 100377a Compare September 3, 2020 14:42
Copy link
Member

@jhadvig jhadvig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Sep 3, 2020
@spadgett
Copy link
Member

spadgett commented Sep 3, 2020

/approve

@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jhadvig, pedjak, spadgett

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 3, 2020
@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

2 similar comments
@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-merge-robot openshift-merge-robot merged commit 2521851 into openshift:master Sep 3, 2020
@openshift-ci-robot
Copy link
Contributor

@pedjak: All pull requests linked via external trackers have merged:

Bugzilla bug 1872369 has been moved to the MODIFIED state.

In response to this:

Bug 1872369: Fix reading TLSConfig for HelmChartRepository

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@spadgett spadgett added this to the v4.6 milestone Sep 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. bugzilla/severity-high Referenced Bugzilla bug's severity is high for the branch this PR is targeting. bugzilla/valid-bug Indicates that a referenced Bugzilla bug is valid for the branch this PR is targeting. component/backend Related to backend lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants