Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
eab992b
Adding bff proxy changes to handle guest auth for playground environm…
shashank26 May 12, 2023
7b1e6c4
Pushing code for web and bff to support login and restricting feature…
shashank26 May 12, 2023
c38944c
Fixing user flow.
shashank26 May 12, 2023
fdc6266
Github API Changes
shashank26 May 12, 2023
2c18995
Api Config changes for github
shashank26 May 12, 2023
38196b1
Adding modal popup to UI
shashank26 May 12, 2023
ae47ad5
Removing github access token from code
shashank26 May 15, 2023
e53785f
Merge branch 'main' into playground
shashank-cloudknit-io May 16, 2023
208802c
updates chart version for bff and web
shashank26 May 16, 2023
1a35341
Updates correct version and reverts wrong change
shashank26 May 16, 2023
21aed7a
hardcodes playground and other env vars for testing.
shashank26 May 16, 2023
1bc84a1
adds logic to extract ip for user
shashank26 May 16, 2023
8a57bec
Adding ipv4 to save user call
shashank26 May 17, 2023
d47c0be
Using correct controller route to fetch user using ip
shashank26 May 17, 2023
4141933
sending ip param to api
shashank26 May 17, 2023
62734d6
Removing error api calls from UI
shashank26 May 18, 2023
5d7127b
Removing error state ref.
shashank26 May 18, 2023
790e894
While Fetching a Playground User, we are trying to associate an org i…
shashank26 May 18, 2023
9fa3403
Using Current Auth for guest login
shashank26 May 19, 2023
8a4cf73
Setting playground environment to true
shashank26 May 19, 2023
99e357f
Correcting the condition to allow routes.
shashank26 May 19, 2023
a730570
Fixing access url for authorized user
shashank26 May 19, 2023
21e1617
Only showing popup to guests
shashank26 May 19, 2023
4729dbc
Testing github commit code.
shashank26 May 19, 2023
055863d
fixing path mapping for github api
shashank26 May 19, 2023
a5a16d8
Removing token from code
shashank26 May 19, 2023
f98d12b
Using ssm to fetch PAT
shashank26 May 20, 2023
b77940d
Fixing success message!
shashank26 May 20, 2023
2b3d62a
Changing the play environment
shashank26 May 22, 2023
2e31c0c
Using demo in the path
shashank26 May 22, 2023
5c36224
Fixing the popup flow
shashank26 May 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CachingService } from './caching/caching.service';
import { ComponentModule } from './component/component.module';
import { EnvironmentModule } from './environment/environment.module';
import { ErrorsModule } from './errors/errors.module';
import { GithubApiModule } from './github-api/github-api.module';
import { AppLoggerMiddleware } from './middleware/logger.middle';
import { OperationsModule } from './operations/operations.module';
import { OrganizationModule } from './organization/organization.module';
Expand Down Expand Up @@ -45,6 +46,7 @@ import { UsersModule } from './users/users.module';
StreamModule,
CachingModule,
ErrorsModule,
GithubApiModule,
],
controllers: [],
providers: [CachingService],
Expand Down
8 changes: 8 additions & 0 deletions api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export type ApiConfig = {
database: string;
};
port: number;
github: {
owner: string,
repo: string,
}
AWS: {
accessKeyId: string;
secretAccessKey: string;
Expand Down Expand Up @@ -62,6 +66,10 @@ export function init() {
database: getEnvVarOrFail('TYPEORM_DATABASE')
},
port: parseInt(process.env.APP_PORT) || 3000,
github: {
owner: getEnvVarOrDefault('GIT_OWNER', 'zlab-tech'),
repo: getEnvVarOrDefault('GIT_REPO', 'hooli-config'),
},
AWS: {
accessKeyId: getEnvVarOrFail('AWS_ACCESS_KEY_ID'),
secretAccessKey: getEnvVarOrFail('AWS_SECRET_ACCESS_KEY'),
Expand Down
3 changes: 2 additions & 1 deletion api/src/environment/environment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ import { SystemService } from 'src/system/system.service';
EnvironmentService,
TeamService,
ReconciliationService,
SystemService
SystemService,
],
exports: [EnvironmentService],
})
export class EnvironmentModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
Expand Down
18 changes: 18 additions & 0 deletions api/src/github-api/github-api.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { GithubApiController } from './github-api.controller';

describe('GithubApiController', () => {
let controller: GithubApiController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [GithubApiController],
}).compile();

controller = module.get<GithubApiController>(GithubApiController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
27 changes: 27 additions & 0 deletions api/src/github-api/github-api.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Controller, Post, Request } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { EnvironmentService } from 'src/environment/environment.service';
import { APIRequest, EnvironmentApiParam } from 'src/types';
import { GithubApiService } from './github-api.service';
import { get } from 'src/config';

@Controller({
version: '1',
})
@ApiTags('github-api')
export class GithubApiController {
constructor(
private readonly envSvc: EnvironmentService,
private readonly gitSvc: GithubApiService
) {}

@Post('/:environmentId')
@EnvironmentApiParam()
async gitCommit(@Request() req: APIRequest) {
const { org, team, env } = req;
const environment = await this.envSvc.findById(org, env.id);
if (environment) {
return this.gitSvc.gitCommit(org, get().github.owner, get().github.repo, `environments/demo/env.yaml`);
}
}
}
22 changes: 22 additions & 0 deletions api/src/github-api/github-api.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common';
import { GithubApiController } from './github-api.controller';
import { EnvironmentService } from 'src/environment/environment.service';
import { EnvironmentMiddleware } from 'src/middleware/environment.middle';
import { GithubApiService } from './github-api.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Environment } from 'src/typeorm/environment.entity';
import { SecretsService } from 'src/secrets/secrets.service';

@Module({
imports: [TypeOrmModule.forFeature([Environment])],
controllers: [GithubApiController],
providers: [EnvironmentService, GithubApiService, SecretsService],
})
export class GithubApiModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(EnvironmentMiddleware).forRoutes({
path: '*/github/:environmentId*',
method: RequestMethod.ALL,
});
}
}
77 changes: 77 additions & 0 deletions api/src/github-api/github-api.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import axios from 'axios';
import { get } from 'src/config';
import { SecretsService } from 'src/secrets/secrets.service';
import { Organization } from 'src/typeorm';

@Injectable()
export class GithubApiService {
private readonly baseUri: string = 'https://api.github.com/repos';
private readonly headers = async (org: Organization) => ({
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${await this.getGITPAT(org)}`,
'X-GitHub-Api-Version': '2022-11-28',
});

constructor(private readonly secretSvc: SecretsService){}

private getURL(owner: string, repo: string, filePath: string) {
return `${this.baseUri}/${owner}/${repo}/contents/${filePath}`;
}

private async getFileSHA(org: Organization, owner: string, repo: string, filePath: string) {
const url = this.getURL(owner, repo, filePath);
const { data } = await axios.get<{ sha: string; content: string }>(url, {
headers: await this.headers(org),
});
return data;
}

public setAlternateFlag(yamlString: string) {
const decodedYaml = Buffer.from(yamlString, 'base64').toString();
console.log(decodedYaml);
if (decodedYaml.includes('teardown: true')) {
return Buffer.from(
decodedYaml.replace('teardown: true', 'teardown: false')
).toString('base64');
}
return Buffer.from(
decodedYaml.replace('teardown: false', 'teardown: true')
).toString('base64');
}

public async gitCommit(org: Organization, owner: string, repo: string, filePath: string) {
const { sha, content } = await this.getFileSHA(org, owner, repo, filePath);
const payload = {
message: 'api testing for playground ',
committer: { name: 'playground', email: 'playground@cloudknit.io' },
content: this.setAlternateFlag(content),
sha,
};

try {
const { data } = await axios.put(
this.getURL(owner, repo, filePath),
payload,
{
headers: await this.headers(org),
}
);

const { commit } = data;
const { html_url } = commit;
return {
status: 'success',
html_url,
};
} catch (err) {
throw new InternalServerErrorException(
'There was an error while pushing the commit to git'
);
}
}

private async getGITPAT(org: Organization) {
return await this.secretSvc.getSsmSecret(org, 'playground-git-token');
}
}
1 change: 1 addition & 0 deletions api/src/organization/organization.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { OrganizationService } from './organization.service';
imports: [TypeOrmModule.forFeature([Organization, User])],
controllers: [OrganizationController],
providers: [OrganizationService, UsersService],
exports: [OrganizationService],
})
export class OrganizationModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
Expand Down
12 changes: 9 additions & 3 deletions api/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { EnvironmentModule } from './environment/environment.module';
import { ComponentModule } from './component/component.module';
import { StreamModule } from './stream/stream.module';
import { ErrorsModule } from './errors/errors.module';
import { GithubApiModule } from './github-api/github-api.module';

export const appRoutes: Routes = [
{
Expand Down Expand Up @@ -58,10 +59,15 @@ export const appRoutes: Routes = [
module: ComponentModule,
},
],
}, {
},
{
path: '/:teamId/github',
module: GithubApiModule,
},
{
path: '/:teamId/errors',
module: ErrorsModule
}
module: ErrorsModule,
},
],
},
],
Expand Down
1 change: 1 addition & 0 deletions api/src/secrets/secrets.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import { SecretsService } from './secrets.service';
@Module({
controllers: [SecretsController],
providers: [SecretsService],
exports: [SecretsService]
})
export class SecretsModule {}
9 changes: 8 additions & 1 deletion api/src/typeorm/User.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
UpdateDateColumn,
} from 'typeorm';
import { Organization } from './Organization.entity';
import { UserRole } from 'src/types';

@Entity({ name: 'users' })
export class User {
Expand Down Expand Up @@ -35,13 +36,19 @@ export class User {
@Column({
default: 'User',
})
role: string;
role: UserRole;

@Column({
default: false,
})
archived: boolean;

@Column({
default: null,
unique: true
})
ipv4: string;

@ManyToMany(() => Organization, (org) => org.users)
organizations: Organization[];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = class UserIpForPlayground1683899743907 {
async up(queryRunner) {
await queryRunner.query(
'ALTER TABLE `USERS` ADD COLUMN `ipv4` varchar(255) UNIQUE default null'
);
}

async down(queryRunner) {}
};
6 changes: 6 additions & 0 deletions api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { Request } from 'express';
import { ComponentReconcile, Environment, EnvironmentReconcile, Team } from './typeorm';
import { Organization } from './typeorm/Organization.entity';

export enum UserRole {
ADMIN = 'Admin',
USER = 'User',
GUEST = 'Guest'
}

export type APIRequest = Request & {
org: Organization;
team: Team;
Expand Down
10 changes: 8 additions & 2 deletions api/src/users/User.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { UserRole } from 'src/types';

export class CreateUserDto {
@ApiProperty()
Expand All @@ -8,14 +9,19 @@ export class CreateUserDto {
email: string;

@ApiProperty({
default: 'User',
default: UserRole.USER,
})
role: string;
role: UserRole;

@ApiProperty()
name: string;
}

export class CreatePlaygroundUserDto {
@ApiProperty()
ipv4: string;
}

export class PatchUserDto {
@ApiProperty({
default: null,
Expand Down
7 changes: 3 additions & 4 deletions api/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import {
Body,
Controller,
Get,
Logger,
NotFoundException,
Param,
Post,
Post
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { AuthController } from 'src/auth/auth.controller';
import { User } from 'src/typeorm/User.entity';
import { CreateUserDto } from './User.dto';
import { UsersService } from './users.service';
Expand All @@ -18,12 +16,12 @@ import { UsersService } from './users.service';
})
@ApiTags('users')
export class UsersController {
private readonly logger = new Logger(AuthController.name);

constructor(private readonly userService: UsersService) {}

@Get('/:username')
public async getUser(@Param('username') username: string): Promise<User> {
console.log(username);
const user = await this.userService.getUser(username);

if (!user) {
Expand All @@ -35,6 +33,7 @@ export class UsersController {

@Post()
public async createUser(@Body() body: CreateUserDto): Promise<User> {
console.log(body);
const user = await this.userService.create(body);

return user;
Expand Down
3 changes: 2 additions & 1 deletion api/src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from 'src/typeorm/User.entity';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
import { Organization } from 'src/typeorm';

@Module({
imports: [TypeOrmModule.forFeature([User])],
imports: [TypeOrmModule.forFeature([User, Organization])],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
Expand Down
Loading