-
Notifications
You must be signed in to change notification settings - Fork 65
refactor: fetch api-key from .env and call agent token API #1473
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
base: main
Are you sure you want to change the base?
refactor: fetch api-key from .env and call agent token API #1473
Conversation
WalkthroughRemoved all token-extraction and token-file creation from provisioning scripts and services. Provisioning now writes/reads only the agent endpoint (CONTROLLER_ENDPOINT). Agent spin-up and common utility use AGENT_API_KEY from environment to request a base token; no token JSON files are created, read, or deleted. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant User
participant ProvSvc as AgentProvisioningService
participant Script as start_agent.sh
participant FS as Filesystem
participant AgentSvc as AgentService
participant Common as CommonService
participant Controller as ControllerEndpoint
User->>ProvSvc: request agent start
ProvSvc->>Script: run start script
Script->>FS: write agent endpoint JSON (CONTROLLER_ENDPOINT)
Note right of Script #f6f3d7: Token extraction/JSON creation removed
ProvSvc->>FS: read endpoint JSON
ProvSvc-->>User: return agentEndPoint
User->>AgentSvc: request spin-up with agentEndPoint
AgentSvc->>Common: getBaseAgentToken(agentEndPoint)
Common->>Common: dotenv loads AGENT_API_KEY
Common->>Controller: POST /token (Authorization: AGENT_API_KEY)
Controller-->>Common: returns token
Common-->>AgentSvc: base token
AgentSvc-->>User: agent registered
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
💤 Files with no reviewable changes (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
libs/common/src/common.service.ts (1)
311-325
: Validate AGENT_API_KEY before use.The
AGENT_API_KEY
is read from the environment without validation. If it's undefined or empty, it will be passed to the Authorization header, potentially causing unclear failures downstream.Apply this diff to add validation:
async getBaseAgentToken(agentEndPoint: string): Promise<string> { const normalizedBaseUrl = await this.normalizeUrlWithProtocol(agentEndPoint); this.logger.log(`Fetching base agent token from ${normalizedBaseUrl}`); const apikey = process.env.AGENT_API_KEY; + if (!apikey) { + throw new BadRequestException('AGENT_API_KEY environment variable is not configured'); + } const agentBaseWalletDetils = await this.httpPost(`${normalizedBaseUrl}${CommonConstants.URL_AGENT_TOKEN}`, '', { headers: { Accept: 'application/json', Authorization: apikey } }); if (!agentBaseWalletDetils) { throw new NotFoundException(ResponseMessages.common.error.fetchBaseWalletToken); } return agentBaseWalletDetils.token; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
apps/agent-provisioning/AFJ/scripts/start_agent.sh
(0 hunks)apps/agent-provisioning/src/agent-provisioning.service.ts
(2 hunks)apps/agent-service/src/agent-service.service.ts
(1 hunks)libs/common/src/common.service.ts
(2 hunks)
💤 Files with no reviewable changes (1)
- apps/agent-provisioning/AFJ/scripts/start_agent.sh
🔇 Additional comments (1)
apps/agent-service/src/agent-service.service.ts (1)
529-529
: LGTM! Token retrieval now environment-based.The updated call to
getBaseAgentToken
with a single argument correctly aligns with the refactored signature that retrieves the API key from environment variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
apps/agent-provisioning/src/agent-provisioning.service.ts (1)
45-45
: Validate AGENT_API_KEY before use
In apps/agent-provisioning/src/agent-provisioning.service.ts (line 45), add a guard to throw a clear error ifprocess.env.AGENT_API_KEY
is undefined instead of passing"undefined"
to the shell script.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
apps/agent-provisioning/src/agent-provisioning.service.ts
(2 hunks)
🔇 Additional comments (1)
apps/agent-provisioning/src/agent-provisioning.service.ts (1)
1-11
: LGTM!The import reorganization and dotenv configuration are clean and follow standard practices.
Signed-off-by: pranalidhanavade <[email protected]>
Signed-off-by: pranalidhanavade <[email protected]>
Signed-off-by: pranalidhanavade <[email protected]>
117068f
to
3ef3ca2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
libs/common/src/common.service.ts (1)
8-18
: Prefer NestJS ConfigModule over dotenv for better DI and testing.While
dotenv.config()
works, NestJS applications typically use@nestjs/config
'sConfigModule
for environment variable management. This provides better dependency injection, type safety, and testability.Consider refactoring to use ConfigModule:
- Install
@nestjs/config
if not already present:npm install @nestjs/config
- Import ConfigModule in your root module (e.g.,
app.module.ts
):import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true, cache: true, }), // other imports ], }) export class AppModule {}
- Inject ConfigService in CommonService:
import { ConfigService } from '@nestjs/config'; @Injectable() export class CommonService { constructor( private readonly httpService: HttpService, private readonly configService: ConfigService ) {} // Use this.configService.get<string>('AGENT_API_KEY') instead of process.env.AGENT_API_KEY }This approach centralizes configuration, improves testing (easier to mock), and provides better type safety.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
.env.demo
(0 hunks).env.sample
(0 hunks)apps/agent-provisioning/AFJ/scripts/docker_start_agent.sh
(1 hunks)apps/agent-provisioning/AFJ/scripts/fargate.sh
(0 hunks)apps/agent-provisioning/AFJ/scripts/start_agent.sh
(1 hunks)apps/agent-provisioning/AFJ/scripts/start_agent_ecs.sh
(0 hunks)apps/agent-provisioning/src/agent-provisioning.service.ts
(2 hunks)apps/agent-service/src/agent-service.service.ts
(1 hunks)libs/common/src/common.service.ts
(2 hunks)
💤 Files with no reviewable changes (4)
- apps/agent-provisioning/AFJ/scripts/start_agent_ecs.sh
- apps/agent-provisioning/AFJ/scripts/fargate.sh
- .env.sample
- .env.demo
🚧 Files skipped from review as they are similar to previous changes (1)
- apps/agent-service/src/agent-service.service.ts
🔇 Additional comments (3)
apps/agent-provisioning/AFJ/scripts/docker_start_agent.sh (1)
228-240
: Clean refactoring: token extraction removed successfully.The endpoint file now contains only
CONTROLLER_ENDPOINT
, and all token-related logic has been removed. The refactoring aligns well with the PR objectives.apps/agent-provisioning/src/agent-provisioning.service.ts (1)
59-79
: Well done: addresses all previous review comments.The implementation now includes:
- Clear error messages with proper grammar
- JSON parsing wrapped in try-catch with specific error handling
- Validation for the presence of
CONTROLLER_ENDPOINT
- Appropriate logging at each step
The refactoring successfully removes token handling while maintaining robust endpoint parsing.
apps/agent-provisioning/AFJ/scripts/start_agent.sh (1)
231-243
: Retain endpoint file creation—it's consumed by the service
The TypeScript service (apps/agent-provisioning/src/agent-provisioning.service.ts) reads and parses this JSON to extract CONTROLLER_ENDPOINT and will error if it’s missing. Removing this logic will break endpoint discovery.
Signed-off-by: pranalidhanavade <[email protected]>
|
What
Summary by CodeRabbit
Refactor
Chores