Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Coscine
backend
apis
Project
Commits
5474e9d7
Commit
5474e9d7
authored
Nov 07, 2019
by
Marcel Nellesen
Browse files
Fix: Included Server side validation on resource setup step
parent
8f1cde13
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/Project/Controllers/DataSourceController.cs
View file @
5474e9d7
using
Coscine.Api.Project.Models
;
using
Coscine.Api.Project.ReturnObjects
;
using
Coscine.ApiCommons
;
using
Coscine.ApiCommons.Factories
;
using
Coscine.ApiCommons.Utils
;
using
Coscine.Configuration
;
using
Coscine.Database.Model
;
...
...
@@ -255,6 +256,66 @@ namespace Coscine.Api.Project.Controllers
}
}
[
HttpPost
(
"[controller]/validate"
)]
public
async
Task
<
IActionResult
>
IsResourceValid
()
{
var
path
=
"/"
;
JToken
resource
=
ObjectFactory
<
JToken
>.
DeserializeFromStream
(
Request
.
Body
);
string
authHeader
=
null
;
if
(
resource
[
"type"
][
"displayName"
].
ToString
().
ToLower
()
==
"rds"
)
{
RDSResourceType
rdsResourceType
=
new
RDSResourceType
();
rdsResourceType
.
BucketName
=
resource
[
"resourceTypeOption"
][
"BucketName"
].
ToString
();
rdsResourceType
.
AccessKey
=
resource
[
"resourceTypeOption"
][
"AccessKey"
].
ToString
();
rdsResourceType
.
SecretKey
=
resource
[
"resourceTypeOption"
][
"SecretKey"
].
ToString
();
authHeader
=
BuildRdsAuthHeader
(
rdsResourceType
);
}
else
if
(
resource
[
"type"
][
"displayName"
].
ToString
().
ToLower
()
==
"gitlab"
)
{
GitlabResourceType
gitlabResourceType
=
new
GitlabResourceType
();
gitlabResourceType
.
RepositoryNumber
=
(
int
)
resource
[
"resourceTypeOption"
][
"RepositoryNumber"
];
gitlabResourceType
.
RepositoryUrl
=
resource
[
"resourceTypeOption"
][
"RepositoryUrl"
].
ToString
();
gitlabResourceType
.
Token
=
resource
[
"resourceTypeOption"
][
"Token"
].
ToString
();
authHeader
=
BuildGitlabAuthHeader
(
gitlabResourceType
);
}
if
(
authHeader
==
null
)
{
return
BadRequest
(
$"No provider for: \"
{
resource
[
"type"
][
"displayName"
].
ToString
()}
\"."
);
}
else
{
// If the path is null, an empty string is added.
string
url
=
$"
{
_configuration
.
GetString
(
"coscine/global/waterbutler_url"
)}{
resource
[
"type"
][
"displayName"
].
ToString
().
ToLower
()}{
path
}
"
;
var
request
=
new
HttpRequestMessage
(
HttpMethod
.
Get
,
url
);
request
.
Headers
.
Authorization
=
new
AuthenticationHeaderValue
(
"Bearer"
,
authHeader
);
// Thread safe according to msdn and HttpCompletionOption sets it to get only headers first.
var
response
=
await
Client
.
SendAsync
(
request
,
HttpCompletionOption
.
ResponseHeadersRead
);
if
(
response
.
IsSuccessStatusCode
)
{
if
(
response
.
Content
.
Headers
.
Contains
(
"Content-Disposition"
))
{
return
File
(
await
response
.
Content
.
ReadAsStreamAsync
(),
response
.
Content
.
Headers
.
GetValues
(
"Content-Type"
).
First
());
}
else
{
var
data
=
JObject
.
Parse
(
await
response
.
Content
.
ReadAsStringAsync
())[
"data"
];
return
Ok
(
new
WaterbutlerObject
(
path
,
data
));
}
}
else
{
return
FailedRequest
(
response
,
path
);
}
}
}
private
IActionResult
FailedRequest
(
HttpResponseMessage
response
,
string
path
)
{
if
(
response
.
StatusCode
==
System
.
Net
.
HttpStatusCode
.
NotFound
)
...
...
@@ -393,9 +454,9 @@ namespace Coscine.Api.Project.Controllers
var
settings
=
new
Dictionary
<
string
,
object
>
{
{
"owner"
,
"Tester"
},
{
"owner"
,
"Tester"
},
{
"repo"
,
gitlabResourceType
.
RepositoryUrl
},
{
"repo_id"
,
gitlabResourceType
.
RepositoryNumber
},
{
"repo_id"
,
gitlabResourceType
.
RepositoryNumber
.
ToString
()
},
{
"host"
,
"https://git.rwth-aachen.de"
}
};
...
...
Ghost User
@ghost
mentioned in commit
7f90a434
·
Nov 07, 2019
mentioned in commit
7f90a434
mentioned in commit 7f90a4347e221a1a0ae1cacd349db2abaf4fa63a
Toggle commit list
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment