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 backend authentication for targetRefs on vmusers by secret #714

Merged

Conversation

mohammadkhavari
Copy link
Contributor

@mohammadkhavari mohammadkhavari commented Aug 1, 2023

This pull request will add backend basic authentication support on vmuser to add them as header on vmauth configuration.
if this approach is accepted I'll add other authentication formats (like bearer) if needed.
#669

@Haleygo
Copy link
Contributor

Haleygo commented Aug 1, 2023

Hello, we are trying to unify the auth config with HTTPAuth, can you reuse it on here?

type HTTPAuth struct {
BasicAuth *BasicAuth `json:"basicAuth,omitempty"`
OAuth2 *OAuth2 `json:"OAuth2,omitempty"`
TLSConfig *TLSConfig `json:"tlsConfig,omitempty"`
*BearerAuth `json:",inline,omitempty"`
// Headers allow configuring custom http headers
// Must be in form of semicolon separated header with value
// e.g.
// headerName:headerValue
// vmalert supports it since 1.79.0 version
// +optional
Headers []string `json:"headers,omitempty"`
}

@mohammadkhavari
Copy link
Contributor Author

Sure.

@f41gh7 f41gh7 requested review from Amper and Haleygo August 10, 2023 14:33
@mohammadkhavari
Copy link
Contributor Author

I'll fix this.

@mohammadkhavari
Copy link
Contributor Author

mohammadkhavari commented Dec 2, 2023

I faced some issues about using HttpAuth in VMuser definition.
It is clear that HttpAuth should place [inline] into TargetRef as the feature request is mentioning it will be transform to the targetRef headers.
And all properties in VMuser's finally should be transformed to vmauth configuration, here goes the challenges I have in writing the logic of this transformation for each field of HttpAuth:

  1. Headers: as it is present already in the targetRef struct it will override the outer Header field, the logic could remain the same . but the description will override by the HttpAuth Headers description that is mentioning wrong format of headers format:

HTTPAuth Headers: semicolon separated header with value e.g. headerName:headerValue
TargetRef Headers: ["header_key: value1,value2"]

  1. BasicAuth, bearerToken:
    It can be implemented using headers configurations the only drawback are passwordFiles field, they usage is specified for CRDs that are leading to deploy a pod and there's filesystem so we can refer a file. But here there's another operation.

  2. About TLSConfig, Oauth: as I have investigated these cannot be satisfy only through headers or current vmauth configuration we have here for each user.

And the logic behind this transformation in something like vmalert is much simpler and straightforward: vmalert crd implements HTTPAuth in remoteWrite section and as we can see theres options(https://docs.victoriametrics.com/vmalert.html ) like -remoteWrite.oauth2.clientID string in vmalert component configuration and operator is reshaping the input configs to component flags.

my recommendation is to first implement HttpAuth features in vmauth component (as I have created issue for basicAuth) and support them in configuration formats then update operator.

What do you think? @Haleygo @f41gh7

And also what is your opinion on passwordFiles? inline basicAuth on CRDS in something like vmalert.notifier.basicAuth.password will finally transform to datasource.basicAuth.passwordFile=/etc/vmalert/remote_secrets/DATASOURCE_BASICAUTHPASSWORD on vmalert component args to hide them in prior info, I think vmauth needs some configuration like this.

@Haleygo
Copy link
Contributor

Haleygo commented Dec 5, 2023

I faced some issues about using HttpAuth in VMuser definition.

@mohammadkhavari Thank you for all the work! Sorry I didn't look too deep when I propose that.
Now I think it's ok to just use BasicAuth here like you already did, I might do some follow up when I work on this later.

@mohammadkhavari
Copy link
Contributor Author

Thank you, Please inform me if any refactoring, code completion, or other modifications, such as adding tests or fixing code, are required.
@Haleygo

@mohammadkhavari
Copy link
Contributor Author

@Haleygo Are there any updates on the feature?

Copy link
Contributor

@Haleygo Haleygo left a comment

Choose a reason for hiding this comment

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

Sorry for the long silence!
Please see comment.

@@ -78,6 +78,9 @@ type TargetRef struct {
// https://docs.victoriametrics.com/vmauth.html#ip-filters
// +optional
IPFilters VMUserIPFilters `json:"ip_filters,omitempty"`
// BasicAuth allow an endpoint to authenticate over basic authentication
// +optional
BasicAuth *BasicAuth `json:"basicAuth,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we don't need to reuse BasicAuth here, the only field missing in vmuser now is Username v1.SecretKeySelector and we can add it directly to VMUserSpec.
Password v1.SecretKeySelector in BasicAuth should already be covered by PasswordRef *v1.SecretKeySelector

// +optional
PasswordRef *v1.SecretKeySelector `json:"passwordRef,omitempty"`

And PasswordFile field is not used.

Copy link
Contributor Author

@mohammadkhavari mohammadkhavari Jan 29, 2024

Choose a reason for hiding this comment

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

There seems to be a bit of confusion regarding the nature of this feature. As far as my usage experience goes, a vmuser can encompass multiple path routes, and each of these paths includes various hosts as backends (though typically, we utilize only one host per targetRef entry). Our objective is to implement basic authentication for these backends. The challenge lies in setting distinct basic authentication credentials for each endpoint, but how we can set basic auth on these endpoints with some field that has appeared on top level field like vmuser's spec itself?

While I've built a custom operator to address our requirements, it has served our needs effectively for quite some time. However, we want to align it with the latest version of VictoriaMetrics and utilize the officially released version to ensure ongoing compatibility.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, sorry, you're right!
But I still think we can add Username v1.SecretKeySelector and Password v1.SecretKeySelector directly instead of reuse BasicAuth since PasswordFile is not used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so should we add it under vmuserSpec.TargetRef ?
It will be a little confusing and not straightforward to know they are for basicAuth? and also it does not seem to be a good solution to provide other methods of http authentication as you've mentioned you're working on.

Copy link
Contributor

Choose a reason for hiding this comment

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

It will be a little confusing and not straightforward to know they are for basicAuth?

I think with description, it's ok.
I'm more against introducing invalid field, which looks like a bug.
What do you think @f41gh7

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree, it's better to use username and password directly. Or wrap it into new struct TargetRefAuth.

It must have username and password for basic Authorization.

E.g.

type TargetRefAuth struct{
  Username v1.SecretKeySelector
  Password v1.SecretKeySelector
}

I think, it less confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think It's good enough to proceed.
I'll go ahead and update the code with the suggested interface.
Do you Agree?

Copy link
Contributor

Choose a reason for hiding this comment

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

@mohammadkhavari yes, please go ahead!

…vmauth config generation

backend basic authentication support on vmuser to add them as header on vmauth configuration it will satisfy the basicauth authorization for bellow issue
VictoriaMetrics#669

* add docs

* add test
@mohammadkhavari
Copy link
Contributor Author

@Haleygo @Amper
Hi, I've updated the implementation due to the suggested interface. A test case has been added, and api.md has been updated as well. I would appreciate it if you could review my changes.

@f41gh7 f41gh7 self-requested a review April 16, 2024 18:46
@f41gh7
Copy link
Collaborator

f41gh7 commented Apr 17, 2024

@Haleygo @Amper Hi, I've updated the implementation due to the suggested interface. A test case has been added, and api.md has been updated as well. I would appreciate it if you could review my changes.

Thanks! I'll take a look soon and most probably, it'll a part of upcoming release.

Copy link
Collaborator

@f41gh7 f41gh7 left a comment

Choose a reason for hiding this comment

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

LGTM

@f41gh7 f41gh7 merged commit 99fbc98 into VictoriaMetrics:master Apr 17, 2024
1 of 2 checks passed
@f41gh7
Copy link
Collaborator

f41gh7 commented Apr 17, 2024

Thanks for contribution!

@mohammadkhavari
Copy link
Contributor Author

mohammadkhavari commented Apr 17, 2024

@f41gh7 According to the failed pipeline, Ive noticed that I missed the refactor structure commit, I have fixed implementations and adopt new interfaces by a new separated commit.
so, make lint make test are also passing locally.
mohammadkhavari@a63743a

should I make another pull req?

@f41gh7
Copy link
Collaborator

f41gh7 commented Apr 17, 2024

@f41gh7 According to the failed pipeline, Ive noticed that I missed the refactor structure commit, I have fixed implementations and adopt new interfaces by a new separated commit. so, make lint make test are also passing locally. mohammadkhavari@a63743a

should I make another pull req?

No worries, I ll make follow-up commit with fix for it.

@mohammadkhavari mohammadkhavari changed the title Add backend authentication for targetRefs on vmusers by secret adapt … Add backend authentication for targetRefs on vmusers by secret Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants