Commit d5380eda authored by Benedikt Heinrichs's avatar Benedikt Heinrichs

Merge branch 'Product/152-connectToRds' into 'master'

New: Handling multiple DataSources

See merge request !13
parents 6abd38a2 76d74c58
......@@ -10,6 +10,16 @@ To use Kestrel you need to add the following NuGet packages:
* ```Microsoft.AspNetCore.Mvc```
* ```Microsoft.AspNetCore```
## Consul keys
The following Consul keys are required:
* "coscine/global/waterbutler_url"
* "coscine/global/rds_access_key"
* "coscine/global/rds_secret_key"
* "coscine/global/gitlabtoken"
* Look at cs/action Consul keys
## Https usage
Generate a developer certificate: ```dotnet dev-certs https```
......
......@@ -10,6 +10,16 @@ To use Kestrel you need to add the following NuGet packages:
* ```Microsoft.AspNetCore.Mvc```
* ```Microsoft.AspNetCore```
## Consul keys
The following Consul keys are required:
* "coscine/global/waterbutler_url"
* "coscine/global/rds_access_key"
* "coscine/global/rds_secret_key"
* "coscine/global/gitlabtoken"
* Look at cs/action Consul keys
## Https usage
Generate a developer certificate: ```dotnet dev-certs https```
......
......@@ -58,8 +58,8 @@
<Reference Include="Coscine.Action, Version=1.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.Action.1.1.2\lib\net461\Coscine.Action.dll</HintPath>
</Reference>
<Reference Include="Coscine.ApiCommons, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.ApiCommons.1.0.0\lib\net461\Coscine.ApiCommons.dll</HintPath>
<Reference Include="Coscine.ApiCommons, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.ApiCommons.1.1.0\lib\net461\Coscine.ApiCommons.dll</HintPath>
</Reference>
<Reference Include="Coscine.Configuration, Version=1.4.0.0, Culture=neutral, PublicKeyToken=ce3d7a32d7dc1e5a, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.Configuration.1.4.0\lib\net461\Coscine.Configuration.dll</HintPath>
......
......@@ -5,7 +5,7 @@
<package id="Castle.Core" version="4.4.0" targetFramework="net472" />
<package id="Consul" version="0.7.2.6" targetFramework="net472" />
<package id="Coscine.Action" version="1.1.2" targetFramework="net472" />
<package id="Coscine.ApiCommons" version="1.0.0" targetFramework="net472" />
<package id="Coscine.ApiCommons" version="1.1.0" targetFramework="net472" />
<package id="Coscine.Configuration" version="1.4.0" targetFramework="net472" />
<package id="Coscine.Database" version="1.4.0" targetFramework="net472" />
<package id="Coscine.ProxyApi" version="1.2.0" targetFramework="net472" />
......
......@@ -20,7 +20,7 @@ namespace Coscine.Api.Project.Controllers
{
private readonly IConfiguration _configuration;
private readonly JWTHandler _jwtHandler;
private static readonly HttpClient _client = new HttpClient();
private static readonly HttpClient Client = new HttpClient();
private readonly Authenticator _authenticator;
private readonly ResourceModel _resourceModel;
......@@ -59,14 +59,11 @@ namespace Coscine.Api.Project.Controllers
return NotFound($"Could not find resource with id: {resourceId}");
}
// Temporary
#if (!DEBUG)
var user = _authenticator.GetUserFromToken();
if (!_resourceModel.OwnsResource(user, resource))
{
return Forbid($"The user does not own the resource {resourceId}");
}
#endif
if (resource.Type == null)
{
......@@ -82,7 +79,7 @@ namespace Coscine.Api.Project.Controllers
}
else if (resource.Type.DisplayName.ToLower() == "gitlab")
{
authHeader = BuildGitlabAuthHeader();
authHeader = BuildGitlabAuthHeader(resource.ExternalId, resource.Url);
}
if (authHeader != null)
......@@ -94,7 +91,7 @@ namespace Coscine.Api.Project.Controllers
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);
var response = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
if (response.Content.Headers.Contains("Content-Disposition"))
......@@ -117,7 +114,7 @@ namespace Coscine.Api.Project.Controllers
else
if (response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
return Forbid($"Not allowed to access the datasource.");
return Forbid("Not allowed to access the datasource.");
}
else
{
......@@ -131,40 +128,61 @@ namespace Coscine.Api.Project.Controllers
}
}
private string BuildWaterbutlerPayload(Dictionary<string, object> auth, Dictionary<string, object> credentials, Dictionary<string, object> settings)
{
var data = new Dictionary<string, object>
{
{ "auth", auth },
{ "credentials", credentials },
{ "settings", settings },
{ "callback_url", "rwth-aachen.de" }
};
var payload = new JwtPayload
{
{ "data", data }
};
return _jwtHandler.GenerateJwtToken(payload);
}
private string BuildRdsAuthHeader(string bucketname)
{
var auth = new Dictionary<string, object>();
var credentials = new Dictionary<string, object>
{
{ "access_key", _configuration.GetString("coscine/global/rds_access_key") },
{ "secret_key", _configuration.GetString("coscine/global/rds_secret_key") }
};
{
{ "access_key", _configuration.GetString("coscine/global/rds_access_key") },
{ "secret_key", _configuration.GetString("coscine/global/rds_secret_key") }
};
var settings = new Dictionary<string, object>
{
{ "bucket", bucketname }
};
var data = new Dictionary<string, object>
{
{ "auth", auth },
{ "credentials", credentials },
{ "settings", settings },
{ "callback_url", "rwth-aachen.de" }
};
var payload = new JwtPayload
{
{ "data", data }
};
{
{ "bucket", bucketname }
};
return _jwtHandler.GenerateJwtToken(payload);
return BuildWaterbutlerPayload(auth, credentials, settings);
}
private string BuildGitlabAuthHeader()
private string BuildGitlabAuthHeader(string externalId, string url)
{
return null;
var auth = new Dictionary<string, object>();
var credentials = new Dictionary<string, object>
{
{ "token", _configuration.GetString("coscine/global/gitlabtoken") }
};
var settings = new Dictionary<string, object>
{
{"owner", "Tester"},
{"repo", url},
{ "repo_id", externalId},
{ "host", "https://git.rwth-aachen.de"}
};
return BuildWaterbutlerPayload(auth, credentials, settings);
}
}
}
......@@ -48,8 +48,8 @@
<Reference Include="Coscine.Action, Version=1.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.Action.1.1.2\lib\net461\Coscine.Action.dll</HintPath>
</Reference>
<Reference Include="Coscine.ApiCommons, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.ApiCommons.1.0.0\lib\net461\Coscine.ApiCommons.dll</HintPath>
<Reference Include="Coscine.ApiCommons, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.ApiCommons.1.1.0\lib\net461\Coscine.ApiCommons.dll</HintPath>
</Reference>
<Reference Include="Coscine.Configuration, Version=1.4.0.0, Culture=neutral, PublicKeyToken=ce3d7a32d7dc1e5a, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.Configuration.1.4.0\lib\net461\Coscine.Configuration.dll</HintPath>
......
......@@ -4,7 +4,7 @@
<package id="AutoMapper.Extensions.Microsoft.DependencyInjection" version="6.0.0" targetFramework="net472" />
<package id="Consul" version="0.7.2.6" targetFramework="net472" />
<package id="Coscine.Action" version="1.1.2" targetFramework="net472" />
<package id="Coscine.ApiCommons" version="1.0.0" targetFramework="net472" />
<package id="Coscine.ApiCommons" version="1.1.0" targetFramework="net472" />
<package id="Coscine.Configuration" version="1.4.0" targetFramework="net472" />
<package id="Coscine.Database" version="1.4.0" targetFramework="net472" />
<package id="Coscine.ProxyApi" version="1.2.0" targetFramework="net472" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment