Skip to content

Commit

Permalink
feat: s3 query cert bindings for origin domain https
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 committed Nov 21, 2024
1 parent abc8494 commit 0d1f63f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
14 changes: 10 additions & 4 deletions kodo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ export class Kodo implements Adapter {
} as DomainWithoutShouldSign));
}

async getOriginDomains(s3RegionId: string, bucket: string): Promise<DomainWithoutShouldSign[]> {
async getOriginDomains(
s3RegionId: string,
bucket: string,
fallbackScheme: 'http' | 'https' = 'http',
): Promise<DomainWithoutShouldSign[]> {
const domainsQuery: Record<string, string | number> = {
bucket,
type: 'source',
Expand Down Expand Up @@ -329,20 +333,22 @@ export class Kodo implements Adapter {
return [];
}

const domainShouldHttps: Record<string, boolean> = {};
const domainShouldHttps: Map<string, boolean> = new Map();
if (
certResult.status === 'fulfilled' &&
Array.isArray(certResult.value.data)
) {
certResult.value.data.forEach((cert: any) => {
domainShouldHttps[cert.domain] = true;
domainShouldHttps.set(cert.domain, true);
});
}

return domainResult.value.data
.map((domain: any) => ({
name: domain.domain,
protocol: domainShouldHttps[domain.domain] ? 'https' : 'http',
protocol: domainShouldHttps.size
? domainShouldHttps.has(domain.domain) ? 'https' : 'http'
: fallbackScheme,
type: 'origin',
apiScope: domain.api_scope === 0 ? 'kodo' : 's3',
}));
Expand Down
51 changes: 12 additions & 39 deletions s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ import {
SdkApiUplogEntry,
} from './uplog';
import { HttpClient, RequestStats } from './http-client';
import { ServiceName } from './kodo-http-client';
import { RegionRequestOptions } from './region';
import { generateReqId } from './req_id';
import { HttpClientResponse } from 'urllib';

export const USER_AGENT = `Qiniu-Kodo-S3-Adapter-NodeJS-SDK/${pkg.version} (${os.type()}; ${os.platform()}; ${os.arch()}; )/s3`;

Expand Down Expand Up @@ -422,45 +420,20 @@ export class S3 extends Kodo {
}

async listDomains(s3RegionId: string, bucket: string): Promise<Domain[]> {
const domainQuery: Record<string, string | number> = {
const fallbackScheme = this.adapterOption.ucUrl?.startsWith('https')
? 'https'
: 'http';
const domains = await this.getOriginDomains(
s3RegionId,
bucket,
type: 'source',
};

let domainResponse: HttpClientResponse<any>;
try {
domainResponse = await this.call({
method: 'GET',
serviceName: ServiceName.Uc,
path: 'domain',
query: domainQuery,
dataType: 'json',
s3RegionId,

// for uplog
apiName: 'queryDomain',
targetBucket: bucket,
});
} catch (err) {
// some server haven't this API. It will cause error.
// ignore it with an empty domain list.
return [];
}

if (!Array.isArray(domainResponse.data)) {
return [];
}

const shouldHttps = this.adapterOption.ucUrl?.startsWith('https');
return domainResponse.data
.filter((domain: any) => domain.api_scope === 1)
.map((domain: any) => ({
name: domain.domain,
protocol: shouldHttps ? 'https' : 'http',
type: 'origin',
apiScope: domain.api_scope === 0 ? 'kodo' : 's3',
fallbackScheme,
);
return domains
.filter(d => d.apiScope === 's3')
.map(d => ({
...d,
private: true,
protected: false,
protected: false
}));
}

Expand Down

0 comments on commit 0d1f63f

Please sign in to comment.