Inedo Community Forums Forums
    • Recent
    • Tags
    • Popular
    • Login

    Welcome to the Inedo Forums! Check out the Forums Guide for help getting started.

    If you are experiencing any issues with the forum software, please visit the Contact Form on our website and let us know!

    Cannot get docker token from server

    Scheduled Pinned Locked Moved Support
    2 Posts 2 Posters 2 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      Stephen.Schaff
      last edited by

      I am having the oddest error. ProGet returns unauthorized when I try to get a docker token from the server that runs my code. But it works fine from my Windows PC and from a random container running in OpenShift (from the terminal window).

      Here is an example of the powershell to get the docker token:

      $ProGetRegistry = "acr.mycompany.net"
      $FeedName = "myfeed"
      $Repo = "runtime/my-container-image"
      $ProGetToken = "123456myToken"
      
      $Bytes  = [System.Text.Encoding]::UTF8.GetBytes("api:${ProGetToken}")
      $Base64 = [Convert]::ToBase64String($Bytes)
      
      $AuthUrl = "https://${ProGetRegistry}/v2/_auth?service=${ProGetRegistry}&scope=repository:${FeedName}/${Repo}:pull"
      
      $AuthHeaders = @{
               Authorization = "Basic $Base64"
      }
      $AuthResponse = Invoke-RestMethod -Uri $AuthUrl -Headers $AuthHeaders -Method Get
      $AuthResponse.token
      

      Like I said, when I run this on my PC or when I get a terminal to a linux container, it works just fine. But when I run it on a JAMS Scheduler agent running windows server 2025, ProGet returns:

      {
        "errors": [
          {
            "code": "DENIED",
            "message": "requested access to the resource is denied",
            "detail": []
          }
        ]
      }
      

      A few other values in the exception that is thrown are:
      Status Code: 403 (Forbidden)
      Server Name: Kestrel

      I have double checked the token I am using and the code (Over and over again.)

      I am SO confused. Why would ProGet give me the token fine on two locations (my Windows 11 PC and Linux container) and not and not on a Windows Server (2025).

      Any ideas?

      1 Reply Last reply Reply Quote
      • atrippA Offline
        atripp inedo-engineer
        last edited by

        Hi @stephen.schaff ,

        The code "looks" okay to me (similar to that reference script we have), but obviously I didn't or test it.

        I know you double-checked the token you're using, but that's the only way I can see that 403 being raised. I suppose the ProGetRegistry could also be wrong.

        But according to the code, there are only 4 cases where that 403 error (i.e. DockerException.Denied()') can occur on the auth handler, and only one of those hit with a Basic api:abc123 header.

        I shared the code below.

        private static Task ProcessAuthAsync(AhHttpContext context)
        {
            // do not allow the response to be stored on shared caches
            context.Response.AddCacheResponseHeaders(HttpCacheability.Private);
        
            var authHeader = context.Request.Headers["Authorization"];
            if (string.IsNullOrWhiteSpace(authHeader) || !authHeader.StartsWith("Basic ", StringComparison.OrdinalIgnoreCase))
            {
                if (string.IsNullOrEmpty(context.Request.QueryString["scope"]) || !string.IsNullOrEmpty(context.Request.QueryString["account"]))
                {
                    context.Response.AppendHeader("WWW-Authenticate", "basic");
                    context.Response.StatusCode = 401;
                    return Task.CompletedTask; 
                }
        
                context.Response.StatusCode = 200;
                context.Response.ContentType = "application/json";
        
                using var jsonWriter = new JsonTextWriter(context.Response.Output);
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("token");
                jsonWriter.WriteValue("anonymous");
                jsonWriter.WriteEndObject();
        
                return Task.CompletedTask;
            }
        
            var basicAuthToken = InedoLib.UTF8Encoding.GetString(Convert.FromBase64String(authHeader.Substring("Basic ".Length).Trim()));
            var userPass = basicAuthToken.Split(new[] { ':' }, 2);
            if (userPass.Length != 2)
                throw DockerException.Denied();
        
            string validatedUserToken;
            if (string.Equals(userPass[0], "api", StringComparison.OrdinalIgnoreCase))
            {
                try { WebApiContext.CreateFromKey(context, userPass[1]); }
                catch { throw DockerException.Denied(); }
                
                validatedUserToken = basicAuthToken;
            }
            else
            {
                if (!LicensingInformation.IsFreeEditionEdgeNodeOrInvalid && !ProGetConfig.Web.UsernamePasswordAuthenticationEnabled)
                    throw DockerException.Denied();
        
        
                if (WebSecurity.GetUser(userPass[0], userPass[1]) == null)
                    throw DockerException.Denied();
        
                validatedUserToken = userPass[0];
            }
        
            var ticket = new AhAuthenticationTicket(1, "docker-bearer", DateTime.UtcNow, DateTime.UtcNow.AddHours(1), true, validatedUserToken);
            context.Response.StatusCode = 200;
            context.Response.ContentType = "application/json";
            using (var jsonWriter = new JsonTextWriter(context.Response.Output))
            {
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("token");
                jsonWriter.WriteValue(AhAuthentication.Encrypt(ticket));
                jsonWriter.WritePropertyName("expires_in");
                jsonWriter.WriteValue((int)(ticket.Expiration - ticket.IssueDate).TotalSeconds);
                jsonWriter.WritePropertyName("issued_at");
                jsonWriter.WriteValue(ticket.IssueDate);
                jsonWriter.WriteEndObject();
            }
        
            return Task.CompletedTask;
        }
        

        Hope that helps. I would try to trigger other error conditions in the code to help preserve your sanity.

        Thanks,
        Alana

        1 Reply Last reply Reply Quote

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • 1 / 1
        • First post
          Last post
        Inedo Website Home • Support Home • Code of Conduct • Forums Guide • Documentation