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

cleanup: Use const values to handle layout / entries well known values and generate strings instead of hardcoding them #626

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

3v1n0
Copy link
Collaborator

@3v1n0 3v1n0 commented Nov 8, 2024

We've various of hardcoded strings around that are often repeated and makes the code hard to maintain and refactor in a safely way.

So replace them with const values and add some utility functions to generate previously hardcoded values.

I've put them into the internal package, but I feel that some of these definitions would be actually better if exported to be re-usable by authd-oidc-brokers too, or at least to an intermediate library for writing authd brokers.

Meanwhile, this makes authd code a bit less fragile and ideally easier to refactor.

UDENG-5162

@3v1n0 3v1n0 requested a review from a team as a code owner November 8, 2024 03:31
@3v1n0 3v1n0 force-pushed the common-consts branch 2 times, most recently from c6d87de to be360ff Compare November 8, 2024 04:31
@codecov-commenter
Copy link

codecov-commenter commented Nov 8, 2024

Codecov Report

Attention: Patch coverage is 93.07958% with 20 lines in your changes missing coverage. Please review.

Project coverage is 83.11%. Comparing base (36f3d0a) to head (375648c).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
examplebroker/broker.go 87.05% 15 Missing and 3 partials ⚠️
pam/internal/adapter/nativemodel.go 94.28% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #626      +/-   ##
==========================================
+ Coverage   82.99%   83.11%   +0.12%     
==========================================
  Files          80       81       +1     
  Lines        8584     8547      -37     
  Branches       75       75              
==========================================
- Hits         7124     7104      -20     
+ Misses       1129     1115      -14     
+ Partials      331      328       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@adombeck adombeck left a comment

Choose a reason for hiding this comment

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

I love these changes! I discussed with Didier during the sprint that I wanted to replace the literal strings we use as map keys with constants. I'm glad you already did that now 😄

internal/brokers/auth/consts.go Outdated Show resolved Hide resolved
internal/brokers/auth/consts.go Outdated Show resolved Hide resolved
internal/brokers/auth/consts.go Show resolved Hide resolved
examplebroker/broker.go Outdated Show resolved Hide resolved
@3v1n0
Copy link
Collaborator Author

3v1n0 commented Nov 8, 2024

I love these changes! I discussed with Didier during the sprint that I wanted to replace the literal strings we use as map keys with constants. I'm glad you already did that now 😄

Yeah, I'm glad you think the same. I had it in the works for a while, maybe we should find a clean way to expose the shared ones with the brokers too, but for now even copying this is fine probably 😅

@adombeck
Copy link
Contributor

adombeck commented Nov 8, 2024

maybe we should find a clean way to expose the shared ones with the brokers too

I was wondering if there is a reason why the broker doesn't depend on authd currently. To me it makes sense to share the constants between authd and the broker. @didrocks, WDYT?

@3v1n0
Copy link
Collaborator Author

3v1n0 commented Nov 11, 2024

maybe we should find a clean way to expose the shared ones with the brokers too

I was wondering if there is a reason why the broker doesn't depend on authd currently. To me it makes sense to share the constants between authd and the broker. @didrocks, WDYT?

Yeah, ideally we should have an intermediate library that provides the shared elements that can be then used both by the example broker and any other go broker out there that we've now and that may show up in future

@3v1n0 3v1n0 requested a review from adombeck November 12, 2024 15:54
Copy link
Member

@denisonbarbosa denisonbarbosa left a comment

Choose a reason for hiding this comment

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

Thanks for addressing this. It's a long-time coming improvement, for sure. Very nice touch dividing the consts into sub-packages so their referencing points towards their meaning.

internal/brokers/auth/consts.go Show resolved Hide resolved
…here

We were duplicating this in the example broker, so let's make the same
content be available everywhere.
Avoid (re)using string values for layout types as is, since they are
prone to errors and they're hard to maintain.
Avoid (re)using string values for entries types as is, since they are
prone to errors and they're hard to maintain.
Avoid building the list manually now that we've consts values and use
this for both optional booleans and supported entries
It can be used to parse optional or required values or entries.

Use this both in broker and example brokers logic
Avoid repeating the same string allover the places
We were using a maps of maps that made code hard to read and maintain
and doing some unneeded conversions to json back and forth.

Avoid this by just using structured data instead
So that we can reuse their ID across the file without further constants
@didrocks
Copy link
Member

maybe we should find a clean way to expose the shared ones with the brokers too

I was wondering if there is a reason why the broker doesn't depend on authd currently. To me it makes sense to share the constants between authd and the broker. @didrocks, WDYT?

Depending on the amount of elements we share. If those are only a few constants, maybe keeping up to date go.mod for everytime we add something will be time consuming. If this is more, than this, yeah, definitively worth doing!

@adombeck
Copy link
Contributor

If those are only a few constants, maybe keeping up to date go.mod for everytime we add something will be time consuming.

IIUC, these are constants which need to be in sync between authd and the broker, so the alternative is that we update them in both repos every time we change something there. I expect that to be more time consuming than only updating them in authd and then running go get github.com/ubuntu/authd in the broker repo.

@adombeck
Copy link
Contributor

IIUC, these are constants which need to be in sync between authd and the broker, so the alternative is that we update them in both repos every time we change something there. I expect that to be more time consuming than only updating them in authd and then running go get github.com/ubuntu/authd in the broker repo.

A mono repo would make this even simpler of course 😁

@didrocks
Copy link
Member

IIUC, these are constants which need to be in sync between authd and the broker, so the alternative is that we update them in both repos every time we change something there. I expect that to be more time consuming than only updating them in authd and then running go get github.com/ubuntu/authd in the broker repo.

Let’s go with this then! Just try to expose as public only the needed bits please!

@3v1n0
Copy link
Collaborator Author

3v1n0 commented Nov 14, 2024

Let’s go with this then! Just try to expose as public only the needed bits please!

Want to handle it in a different PR or here?

@didrocks
Copy link
Member

Let’s go with this then! Just try to expose as public only the needed bits please!

Want to handle it in a different PR or here?

As you are adding the consts here, let’s expose them directly publicly

@3v1n0
Copy link
Collaborator Author

3v1n0 commented Nov 14, 2024

Ok... Will move them.

Ideally I would have used a different strategy though: we could have used proto files enums and expose them instead (after a rename) and use the same strategy to generate the JSON that is passed through dbus too (more or less how it happens with gdm), as it would have saved us lots of manual management of the types and the usage of map[string][string] in the broker side.

But I'm unsure if that's too late now.

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.

5 participants