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

Mounting secret - reading credentials from file #746

Merged
merged 1 commit into from
Sep 27, 2024

Conversation

GunaKKIBM
Copy link
Contributor

What this PR does / why we need it:
Rather than getting apikey from env, the secret needs to be mounted inside controller and node server pod according CIS kubernetes benchmark

Which issue(s) this PR fixes:
#727

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Sep 16, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @GunaKKIBM!

It looks like this is your first PR to kubernetes-sigs/ibm-powervs-block-csi-driver 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/ibm-powervs-block-csi-driver has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Sep 16, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @GunaKKIBM. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Sep 16, 2024
@GunaKKIBM
Copy link
Contributor Author

Will update the tests if the code changes are ok.

@@ -60,7 +61,16 @@ func NewPowerVSCloud(cloudInstanceID, zone string, debug bool) (Cloud, error) {
}

func newPowerVSCloud(cloudInstanceID, zone string, debug bool) (Cloud, error) {
apikey := os.Getenv("IBMCLOUD_API_KEY")
Copy link
Member

Choose a reason for hiding this comment

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

I see you are completely removing the reading the API key from the env which is a breaking change, please keep both the ways.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@mkumatag
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Sep 18, 2024
@@ -252,3 +256,31 @@ func (p *powerVSCloud) GetDiskByID(volumeID string) (disk *Disk, err error) {
CapacityGiB: int64(*v.Size),
}, nil
}

func readCredentials() (string, error) {
apiKey, err := readCredentialsFromFile()
Copy link
Member

Choose a reason for hiding this comment

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

you are ignoring all the errors which might be problematic when program genuinely failed to read the file content.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://github.com/kubernetes-sigs/ibm-powervs-block-csi-driver/pull/746/files#diff-27059fbf8e5d8f0370d7993eabd010230eb1cef170edd78aea391fb6f55953b2R285 - updated here, other than, is not exists, any other reading error, it is returning error, but ideally, If API_KEY_PATH is defined and if the file does not exist, that should also error out I think. Please let me know your thought.

Copy link
Member

Choose a reason for hiding this comment

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

yes, file not found should also be error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

return apiKey, nil
}

apikey := os.Getenv("IBMCLOUD_API_KEY")
Copy link
Member

Choose a reason for hiding this comment

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

same variable name with just a smallcase may create a confusion, you can reuse the first variable itself

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I didn't understand this comment, earlier the env variable that was used in the code (in current main branch), is IBMCLOUD_API_KEY itself

Copy link
Member

Choose a reason for hiding this comment

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

I meant this

Suggested change
apikey := os.Getenv("IBMCLOUD_API_KEY")
apiKey = os.Getenv("IBMCLOUD_API_KEY")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Missed it for a typo. Updated

@@ -252,3 +256,31 @@ func (p *powerVSCloud) GetDiskByID(volumeID string) (disk *Disk, err error) {
CapacityGiB: int64(*v.Size),
}, nil
}

func readCredentials() (string, error) {
apiKey, err := readCredentialsFromFile()
Copy link
Member

Choose a reason for hiding this comment

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

adding a warning logging is better instead of no logs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -42,6 +42,7 @@ const (
PollInterval = 5 * time.Second
VolumeInUseState = "in-use"
VolumeAvailableState = "available"
secretPath = "/var/secrets/IBMCLOUD_API_KEY"
Copy link
Member

Choose a reason for hiding this comment

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

where is this been used? I'm not sure why such mistakes aren't caught in the CI..

created #751 to track

key: IBMCLOUD_API_KEY
optional: true
- name: API_KEY_PATH
value: /var/secrets/IBMCLOUD_API_KEY
Copy link
Member

Choose a reason for hiding this comment

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

is /var a standard path for the credential?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I know, there's no one standard path as such, it could be /etc, /var, /mnt.

Copy link
Member

Choose a reason for hiding this comment

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

/etc looks to be better place..


func readCredentialsFromFile() (string, error) {
apiKeyPath := os.Getenv("API_KEY_PATH")
if len(apiKeyPath) == 0 {
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 len(apiKeyPath) == 0 {
if apiKeyPath == "" {

if len(apiKey) != 0 {
return apiKey, nil
}

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
klog.Info("Falling back to read IBMCLOUD_API_KEY environment variable for the key")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@mkumatag
Copy link
Member

overall LGTM, please squash all the commits.

@yussufsh @kishen-v ptal..

@kishen-v
Copy link
Contributor

Hi @GunaKKIBM,
Thanks for the PR.
/lgtm

@k8s-ci-robot
Copy link
Contributor

@kishen-v: changing LGTM is restricted to collaborators

In response to this:

Hi @GunaKKIBM,
Thanks for the PR.
/lgtm

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-sigs/prow repository.

klog.Info("Falling back to read IBMCLOUD_API_KEY environment variable for the key")
apiKey = os.Getenv("IBMCLOUD_API_KEY")
if apiKey == "" {
return "", fmt.Errorf("IBMCLOUD_API_KEY is not provided")
Copy link
Contributor

@kishen-v kishen-v Sep 20, 2024

Choose a reason for hiding this comment

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

nit:
We could switch to errors.New as the error string is static + has minor gains in op time. We can track this in a separate issue, altogether.

Suggested change
return "", fmt.Errorf("IBMCLOUD_API_KEY is not provided")
return "", errors.New("IBMCLOUD_API_KEY is not provided")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#755 - created a GHE to track this

Comment on lines +67 to +69
env:
- name: API_KEY_PATH
value: /etc/secrets/IBMCLOUD_API_KEY
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason for adding the key to this controller?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After looking unto the code, it looks like, the node update controller also needs the apikey (it is also initialising the authenticator), this apikey is needed to GetPVMInstancedetails. Hence, added here too

@yussufsh
Copy link
Contributor

@GunaKKIBM please squash your commits. Overall the changes looks good.

Copy link
Contributor

@yussufsh yussufsh left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 27, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: GunaKKIBM, yussufsh

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 27, 2024
@k8s-ci-robot k8s-ci-robot merged commit 7838d84 into kubernetes-sigs:main Sep 27, 2024
6 checks passed
@yussufsh yussufsh linked an issue Oct 9, 2024 that may be closed by this pull request
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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CSI driver can't read credentials from file
5 participants