Unable to modify a Tenant using the Octopus.Client .NET library


(Keith Sohl) #1

Hi,

I am using the Octopus.Client .NET library to manage Octopus through the API. I need to add a tag to a Tenant but I get an error when I try to obtain a TenantEditor for a existing Tenant. Here is the code;

IOctopusAsyncClient octopusAsyncClient = myOctopusClientFactory.CreateAsyncClient(new OctopusServerEndpoint(octopusServerAddress, apiKey));
var octopusAsyncRepository = octopusAsyncClient.Repository;
var tenantEditor = await octopusAsyncRepository.Tenants.CreateOrModify(tenantName);

And the error (thrown at the third line above):

There was a problem with your request.

 - A tenant with this name already exists. Please choose a different name.

Stack Trace:
   at Octopus.Client.OctopusAsyncClient.<DispatchRequest>d__54`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Octopus.Client.OctopusAsyncClient.<Create>d__42`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Octopus.Client.Editors.Async.TenantEditor.<CreateOrModify>d__9.MoveNext()

The same two lines of code work fine when the Tenant does not already exist; for the “Create” case, in other words.

I cannot see what else I can do to allow modification of the Tenant. How do I use Octopus.Client to modify a Tenant once it has been created?

Many thanks for your help, in advance!

Keith


(Nick Josevski) #2

Hi Keith,

Thanks for getting in touch. We had 2 ideas for what this might be. I reproduced the issue you’re seeing when the user doing the API call had sufficient Tenant related permissions like create, but lacked permission to see that particular tenant. Can you check the access level the user doing this has, via the Test Permissions page, the answer may be to use a more privileged user.

The other is a strange case sensitivity can you check/share the name of your Tenant in the script versus how it is in the app, and test if matching the case exactly matters. But I could not reproduce it this way. If your user does not have any permissions restrictions please test this avenue, or share with us the exact casing in app and in your code so we can try more.

Here’s my code just as example that let me reproduce the error There was a problem with your request. - A tenant with this name already exists. Please choose a different name. with insufficient permissions:


	var myOctopusClientFactory = new OctopusClientFactory();
	IOctopusAsyncClient octopusAsyncClient = await myOctopusClientFactory.CreateAsyncClient(new OctopusServerEndpoint(octopusServerAddress, userWithRestrictedTenantAccess));
	var octopusAsyncRepository = octopusAsyncClient.Repository;
	var tenantEditor = await octopusAsyncRepository.Tenants.CreateOrModify(tenantName);

	await tenantEditor.Save();

	await octopusAsyncRepository.Tenants.GetAll().Dump();

Here’s my test users permissions, they have TenantView restricted on a subset of the Tenants, but TenantCreate is not:

Regards,
Nick


(Keith Sohl) #3

Hi Nick,

Thank you for your prompt reply, it was much appreciated.

I have investigated the user’s permissions as you requested but this really does not seem to be the cause of the problem. Here is why:


These are the permissions that I have granted to the user.

I first performed a test to make sure that I was working with the correct User Role. When I REVOKE the TenantCreate permission, then I am unable to create a new Tenant through the API. However, I get a very different error to the one that I put in my original post:

You do not have permission to perform this action. Please contact your Octopus administrator. Missing permission: TenantCreate

I then restored the TenantCreate permission and revoked the TenantView permission. I get the following error when my code then attempts to modify the Tenant. The errors is once again on the call to octopusAsyncRepository.Tenants.CreateOrModify(tenantName):

You do not have permission to perform this action. Please contact your Octopus administrator. Missing permission: TenantView

Source: Octopus.Client

Stack Trace:
   at Octopus.Client.OctopusAsyncClient.<DispatchRequest>d__54`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Octopus.Client.OctopusAsyncClient.<Get>d__33`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Octopus.Client.OctopusAsyncClient.<List>d__38`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Octopus.Client.OctopusAsyncClient.<Paginate>d__40`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Octopus.Client.Repositories.Async.BasicRepository`1.<FindOne>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Octopus.Client.Editors.Async.TenantEditor.<CreateOrModify>d__9.MoveNext()

In other words I am unable to replicate what your results; I do not get my original error (" - A tenant with this name already exists. Please choose a different name.") by revoking TenantView.

As I final test I restored the TenantView permission. I got back my original error again.


You also said that the exact casing of the name of the Tenant might be the cause. I am uncertain how this could be a factor; the phrase “this name already exists” in the error implies that the names match as expected. If I had the wrong case, then I would expect to to create a new Tenant.

For the record, here is the name:

[DEV][Test Org 2][FFFFFFFF7BA543CF8A6572BB0F7A43C0]

Have you got any other suggestions for what could be preventing the Octopus.Client from modifying an existing Tenant?

Thanks,
Keith


(Nick Josevski) #4

Hi Keith,

Thanks for the detailed information and trying a few things. Once I put some square brackets into my tenant names, I reproduced the issue you are experiencing It looks to be related to the square brackets. We’re still investigating.

In the mean time, if you can, you could change your naming convention to use round brackets instead, e.g. this worked: (DEV)(Test Org 2)(FFFFFFFF7BA543CF8A6572BB0F7A43C0) fine in the same Octopus.Client code.

Here is the issue we’ve raised so you can follow along, we’re still investigating why it’s failing, the code at this point looks like it should be able to handle it: https://github.com/OctopusDeploy/Issues/issues/5218

As a separate question, is there a reason you have a few dimensions to the name of a Tenant including what looks to be an environment? Have you discovered/used the Tenant Tag feature in Octopus, it may be an easier way to achieve organizing Tenants? Maybe it can let you simplify the Tenant names too.

Regards,
Nick


(Keith Sohl) #5

Hi Nick,

Thanks for your response. It is good to know that you are closer to understanding the root cause of our issue. I was time constrained today but tomorrow I will take a few minutes to experiment with our deployment code in the DEV environment to confirm that I can add the Tag through the API if I avoid square brackets in the name. That should confirm that we are definitely on the right track.

It is worth noting, however, that we have used that naming convention across all of our environments (DEV, TEST, STAGING and PRODUCTION) so it will be difficult for us to make a switch to use round brackets. It would therefore be great if the reason for the failure were found quickly so that we could patch our Octopus server to fix the issue.

And as for your last question, about the Tenant Tag feature, we are indeed aware of it and we are using it to label our Tenants with the environment (and other attributes). The naming convention ("[DEV]") is just an added layer of “clarity”. It helps to group each environment together in the UI, for example.

Thanks,
Keith


(Keith Sohl) #6

Hi Nick,

I have a little more information for you on this issue. First of all I have confirmed that if I remove the square brackets completely from the name then I do not see the issue; I can modify the Tenant by adding a Tag to it. Good news!

Noticing that the issue in GitHub is described as “Searching for resources with an ODD amount of left square brackets ‘[’ delivers non-predictable or missing results”, I tried with an EVEN number of left square brackets, e.g. “[DEV][Test Org 2][FFFFFFFF7BA543CF8A6572BB0F7A43C0][xxx]”. Unfortunately I still got the error. You might want to double-check your reproduction of the issue, it might be a more fundamental inability to handle names with square brackets.

Lastly, can you please provide an approximate timescale for when this issue might be resolved? If it is likely to be just a few days then my team and I can afford to wait. If it is much longer than that then we shall have to find an alternative solution.

Many thanks,
Keith


(Nick Josevski) #7

Hi Keith,

Thanks for doing some more digging on the issue, you are right it wasn’t just about an odd amount. I’ve updated the issue. The fix was to ensure proper escaping of the left bracket which was for most code paths, except for a few edge cases that impacted loading tenants, tags and environments.

We’ve just shipped the patch for this in 2018.11.3 it’s available at https://octopus.com/downloads

I tested it with your exact tenant names, so do let us know if you still have issues after updating your instance.

Regards,
Nick