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 GET endpoint to trigger copy of instance #224

Merged
merged 5 commits into from
May 2, 2023
Merged

Conversation

SandGrainOne
Copy link
Member

Description

Adding a new GET endpoint in InstanceController. The new endpoint is intended to be an easy to access function to create a new instance based on an existing instance. Data is copied from one to the other based on CopyInstanceSettings.

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)
  • All tests run green

Documentation

  • User documentation is updated with a separate linked PR in altinn-studio-docs. (if applicable)

Comment on lines +771 to +774
catch (Exception altinnAppException)
{
throw new ServiceException(HttpStatusCode.InternalServerError, $"App.GetAppModelType failed: {altinnAppException.Message}", altinnAppException);
}

Check notice

Code scanning / CodeQL

Generic catch clause

Generic catch clause.
@ivarne
Copy link
Member

ivarne commented Mar 30, 2023

Doesn’t mutating apis usually try to avoid using the GET method?

@tjololo tjololo self-assigned this Apr 12, 2023
@tjololo
Copy link
Member

tjololo commented Apr 14, 2023

Doesn’t mutating apis usually try to avoid using the GET method?

To explain the GET suggestion it is due to technical challenges in Altinn2. We need to have a discussion in the team as to how we should solve this.
I think it's a bad idea to have technical dept in an legacy system result in technical dept in a new system.

@ivarne
Copy link
Member

ivarne commented Apr 14, 2023

There are two standard ways this could work without xsrf issues.

  1. "altinn2" does an authenticated backchannel call to the app to an instantiation endpoint that accepts an existing instance to copy from and then redirects the user to the correct url for the newly created instance.
  2. "altinn2" redirects the user to a get endpoint that does no mutation, but then asks the user to trigger a Post with the action.

I get that the first might be limited by altinn2 capabilities, but the manual confirmation step is just as simple on the altinn 2 side. It should probably be implemented in app-frontend-react, but could just be a simple form a user might click submit on.
Just redirecting a user to a GET endpoint that does the mutation was much worse before "same-orgin" cookies, but still don't feel like a safe thing to do.

Copy link
Member

@tjololo tjololo left a comment

Choose a reason for hiding this comment

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

Not happy with mutating GET endpoint, but as the old system has limitations we would need to accept technical debt somewhere. By accepting the dept here we would not introduce dept in both frontend and backend

src/Altinn.App.Api/Controllers/InstancesController.cs Outdated Show resolved Hide resolved
@ivarne
Copy link
Member

ivarne commented Apr 21, 2023

See also the suggestion in Altinn/app-frontend-react#286

The workflow should be that triggering copy in SBL should send a request to app frontend. That again builds the request to app backend and renders the newly created instance.

Personally I think frontend should ask the user for a confirmation that they want to copy the instance (unless altinn2 is able to provide a signed assertion), so that there isn't any risk that third party websites can trigger copies with user cookies without user involvement.

_instantiationValidator.Setup(v => v.Validate(It.IsAny<Instance>())).ReturnsAsync(instantiationValidationResult);

// Act
ActionResult actual = await SUT.CopyInstance("ttd", "copy-instance", instanceOwnerPartyId, instanceGuid);

Check warning

Code scanning / CodeQL

Call to obsolete method

Call to obsolete method [CopyInstance](1).
_appMetadata.Setup(a => a.GetApplicationMetadata()).ReturnsAsync(application);

// Act
ActionResult actual = await SUT.CopyInstance("ttd", "copy-instance", 343234, Guid.NewGuid());

Check warning

Code scanning / CodeQL

Call to obsolete method

Call to obsolete method [CopyInstance](1).
.ReturnsAsync(CreateXacmlResponse("Deny"));

// Act
ActionResult actual = await SUT.CopyInstance("ttd", "copy-instance", 343234, Guid.NewGuid());

Check warning

Code scanning / CodeQL

Call to obsolete method

Call to obsolete method [CopyInstance](1).
.ReturnsAsync(instance);

// Act
ActionResult actual = await SUT.CopyInstance("ttd", "copy-instance", instanceOwnerPartyId, instanceGuid);

Check warning

Code scanning / CodeQL

Call to obsolete method

Call to obsolete method [CopyInstance](1).
_processEngine.Setup(p => p.StartTask(It.IsAny<ProcessChangeContext>()));

// Act
ActionResult actual = await SUT.CopyInstance(Org, AppName, InstanceOwnerPartyId, instanceGuid);

Check warning

Code scanning / CodeQL

Call to obsolete method

Call to obsolete method [CopyInstance](1).
.ReturnsAsync(CreateApplicationMetadata($"{Org}/{AppName}", false));

// Act
ActionResult actual = await SUT.CopyInstance("ttd", "copy-instance", 343234, Guid.NewGuid());

Check warning

Code scanning / CodeQL

Call to obsolete method

Call to obsolete method [CopyInstance](1).
_httpContextMock.Setup(httpContext => httpContext.User).Returns(PrincipalUtil.GetOrgPrincipal("ttd"));

// Act
ActionResult actual = await SUT.CopyInstance("ttd", "copy-instance", 343234, Guid.NewGuid());

Check warning

Code scanning / CodeQL

Call to obsolete method

Call to obsolete method [CopyInstance](1).
@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 8 Code Smells

62.1% 62.1% Coverage
0.0% 0.0% Duplication

Copy link
Member

@tjololo tjololo left a comment

Choose a reason for hiding this comment

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

:godmode:

@tjololo tjololo added the feature Label Pull requests with new features. Used when generation releasenotes label May 2, 2023
@tjololo tjololo merged commit c3b5fa8 into main May 2, 2023
@tjololo tjololo deleted the feature/NewCopyEndpoint branch May 2, 2023 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Label Pull requests with new features. Used when generation releasenotes
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants