From e901745be5e358faf671353f9d7e7cb1ffdc10b9 Mon Sep 17 00:00:00 2001 From: Mustafa Gezen Date: Fri, 17 Feb 2023 19:31:11 +0100 Subject: [PATCH] Support simple filter for Build API --- hydra/pkg/hydra/autosignup.mjs | 53 ++++++++++++++++++++++------------ peridot/db/models/build.go | 12 ++++---- peridot/db/psql/build.go | 8 ++++- peridot/proto/v1/build.proto | 2 ++ peridot/ui/server/index.mjs | 2 +- 5 files changed, 50 insertions(+), 27 deletions(-) diff --git a/hydra/pkg/hydra/autosignup.mjs b/hydra/pkg/hydra/autosignup.mjs index 869e580..1463cb1 100644 --- a/hydra/pkg/hydra/autosignup.mjs +++ b/hydra/pkg/hydra/autosignup.mjs @@ -36,16 +36,21 @@ import { svcNameHttp, endpointHttp, - NS, envOverridable, + NS, } from '../../../common/frontend_server/upstream.mjs'; import pkg from '@ory/hydra-client'; -import os from 'os'; -const { Configuration, PublicApi, AdminApi } = pkg; +const { Configuration, OidcApi, OAuth2Api } = pkg; export function hydraPublicUrl() { return envOverridable('hydra_public', 'http', () => { + if (!process.env['RESF_ENV']) { + if (process.env['HYDRA_PUBLIC_URL']) { + return process.env['HYDRA_PUBLIC_URL']; + } + return 'https://hdr-dev.internal.rdev.ciq.localhost'; + } const svc = svcNameHttp('hydra-public'); return endpointHttp(svc, NS('hydra-public'), ':4444'); }); @@ -53,18 +58,21 @@ export function hydraPublicUrl() { function hydraAdminUrl() { return envOverridable('hydra_admin', 'http', () => { + if (!process.env['RESF_ENV']) { + return 'https://hdr-admin-dev.internal.rdev.ciq.localhost'; + } const svc = svcNameHttp('hydra-admin'); return endpointHttp(svc, NS('hydra-admin'), ':4445'); }); } -const hydraAdmin = new AdminApi( +const hydraAdmin = new OAuth2Api( new Configuration({ basePath: hydraAdminUrl(), }) ); -export const hydraPublic = new PublicApi( +export const hydraPublic = new OidcApi( new Configuration({ basePath: hydraPublicUrl(), }) @@ -85,6 +93,16 @@ function secret() { } export async function hydraAutoSignup(req) { + const envNameClientID = `${req.client.toUpperCase()}_CLIENT_ID`; + const envNameClientSecret = `${req.client.toUpperCase()}_CLIENT_SECRET`; + + if (process.env[envNameClientID] && process.env[envNameClientSecret]) { + return { + clientID: process.env[envNameClientID], + secret: process.env[envNameClientSecret], + }; + } + let ns = process.env['RESF_NS']; if (!ns || ns === '') { ns = 'dev'; @@ -96,11 +114,11 @@ export async function hydraAutoSignup(req) { } const clientModel = { client_name: name, - client_id: serviceName, scope: req.scopes, client_secret: secret(), redirect_uris: null, grant_types: ['authorization_code', 'refresh_token'], + owner: serviceName, }; if (req.frontend) { clientModel.redirect_uris = [req.redirectUri]; @@ -108,23 +126,20 @@ export async function hydraAutoSignup(req) { } const ret = { - clientID: serviceName, secret: secret(), }; - try { - await hydraAdmin.getOAuth2Client(serviceName); - try { - console.log(`Updated client ${name}`); - await hydraAdmin.updateOAuth2Client(serviceName, clientModel); - } catch (e) { - // noinspection ExceptionCaughtLocallyJS - throw e; - } - } catch (e) { - console.log(`Created client ${name}`); - await hydraAdmin.createOAuth2Client(clientModel); + const resp = await hydraAdmin.listOAuth2Clients(undefined, undefined, undefined, serviceName); + + let client; + if (resp.data.length <= 0) { + client = await hydraAdmin.createOAuth2Client(clientModel); + } else { + client = resp.data[0]; + await hydraAdmin.setOAuth2Client(client.client_id, clientModel); } + ret.clientID = client.client_id; return ret; } + diff --git a/peridot/db/models/build.go b/peridot/db/models/build.go index 0fed81d..2e06b09 100644 --- a/peridot/db/models/build.go +++ b/peridot/db/models/build.go @@ -63,16 +63,16 @@ func (b *Build) ToProto() (*peridotpb.Build, error) { var ir []*peridotpb.ImportRevision if b.TaskResponse.Valid && b.TaskStatus == peridotpb.TaskStatus_TASK_STATUS_SUCCEEDED { - anyResponse := anypb.Any{} - err := protojson.Unmarshal(b.TaskResponse.JSONText, &anyResponse) + anyResponse := &anypb.Any{} + err := protojson.Unmarshal(b.TaskResponse.JSONText, anyResponse) if err != nil { return nil, err } if anyResponse.TypeUrl == "type.googleapis.com/resf.peridot.v1.ModuleBuildTask" { - taskResponse := peridotpb.ModuleBuildTask{} + taskResponse := &peridotpb.ModuleBuildTask{} - err = anyResponse.UnmarshalTo(&taskResponse) + err = anyResponse.UnmarshalTo(taskResponse) if err != nil { return nil, err } @@ -81,9 +81,9 @@ func (b *Build) ToProto() (*peridotpb.Build, error) { ir = append(ir, stream.ImportRevision) } } else if anyResponse.TypeUrl == "type.googleapis.com/resf.peridot.v1.SubmitBuildTask" { - taskResponse := peridotpb.SubmitBuildTask{} + taskResponse := &peridotpb.SubmitBuildTask{} - err = anyResponse.UnmarshalTo(&taskResponse) + err = anyResponse.UnmarshalTo(taskResponse) if err != nil { return nil, err } diff --git a/peridot/db/psql/build.go b/peridot/db/psql/build.go index 0afaceb..648f6c5 100644 --- a/peridot/db/psql/build.go +++ b/peridot/db/psql/build.go @@ -112,6 +112,10 @@ func (a *Access) ListBuilds(filters *peridotpb.BuildFilters, projectId string, p if filters == nil { filters = &peridotpb.BuildFilters{} } + var packageName *string + if filters.PackageName != nil { + packageName = &filters.PackageName.Value + } var ret models.Builds err := a.query.Select( @@ -135,11 +139,13 @@ func (a *Access) ListBuilds(filters *peridotpb.BuildFilters, projectId string, p where b.project_id = $1 and ($2 :: int is null or $2 :: int = 0 or t.status = $2 :: int) + and ($3 :: text is null or $3 :: text = '' or p.name = $3 :: text) order by b.created_at desc - limit $3 offset $4 + limit $4 offset $5 `, projectId, filters.Status, + packageName, limit, utils.GetOffset(page, limit), ) diff --git a/peridot/proto/v1/build.proto b/peridot/proto/v1/build.proto index 220056c..96e793f 100644 --- a/peridot/proto/v1/build.proto +++ b/peridot/proto/v1/build.proto @@ -199,6 +199,8 @@ message BuildFilters { // The status filter only returns builds that // has the given status TaskStatus status = 1; + + google.protobuf.StringValue package_name = 2; } message ListBuildsRequest { diff --git a/peridot/ui/server/index.mjs b/peridot/ui/server/index.mjs index f85fa38..a906a9b 100644 --- a/peridot/ui/server/index.mjs +++ b/peridot/ui/server/index.mjs @@ -48,7 +48,7 @@ export default async function run(webpackConfig) { const envPublicUrl = process.env['PERIDOT_FRONTEND_HTTP_PUBLIC_URL']; const frontendUrl = process.env['RESF_NS'] ? envPublicUrl : devFrontendUrl; - const wellKnown = await hydraPublic.discoverOpenIDConfiguration(); + const wellKnown = await hydraPublic.discoverOidcConfiguration(); const hdr = await hydraAutoSignup({ name: 'Peridot', client: 'peridot',