Uploaded image for project: 'Insights Experiences'
  1. Insights Experiences
  2. HMS-3921

Refactor token code in image builder

XMLWordPrintable

    • Icon: Task Task
    • Resolution: Unresolved
    • Icon: Undefined Undefined
    • None
    • None
    • Image Builder
    • None

      Image builder HTTP clients has duplicated code for OAuth2 tokens which can be turned into a generic component, tested separately and then clients can be simplified to reuse the same component. The logic does not take expiration in to account making some requests twice which makes testing harder.

      Refactoring task:

      Create a new file lazy_token.go, find it a good package (a place to live in). Design a new type called LazyToken that will have the following functionality:

      • NewLazyToken(url, clientId, clientSecret) which creates a new value
      • Token(context.Context) string method that will check if token exists already and is not expiring in 1 minute or not expired. in other case it will require a new token, stores expiration time as well as the token itself and return it
      • Token method is protected by mutex
      • when token is reused, issue a debug log statement through context logger (also include expiration time)
      • when token is requested for any reason, issue a info log statement through context logger (also include expiration time)
      • cover the LazyToken with tests, test all possible use cases
      • Create an interface named Tokener with the Token(ctx) method
      • refactor all HTTP clients to make use of the Tokener interface (tokener is being passed into a client)
      • Create a dummy implementation of Tokener that will return just a string "testtoken"
      • Use this dummy implementation in all tests (you need to make it possible to pass Tokener into a client) - tests can be now simplified there is no reason to test everything twice (once for 401 once for 403)

            mgold@redhat.com Michal Gold
            rhn-engineering-lzapletal Lukáš Zapletal
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: