Skip to content
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

Support for self-signed certificates #565

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions packages/apollo-cli/src/commands/schema/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export default class SchemaDownload extends Command {
description:
"The URL of the server to fetch the schema from or path to ./your/local/schema.graphql"
}),
skipSSLValidation: flags.boolean({
char: "k",
description: "Allow connections to a SSL site without certs"
}),

...engineFlags
};
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface EndpointConfig {
url?: string; // main HTTP endpoint
subscriptions?: string; // WS endpoint for subscriptions
headers?: Object; // headers to send when performing operations
skipSSLValidation?: boolean; // bypass the SSL validation on a HTTPS request
}

export interface SchemaDependency {
Expand Down
48 changes: 31 additions & 17 deletions packages/apollo-cli/src/fetch-schema.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { extractDocumentFromJavascript } from "apollo-codegen-core/lib/loading";
import { fs } from "apollo-codegen-core/lib/localfs";
import * as path from "path";
import fetch from "node-fetch";
import gql from "graphql-tag";
import {
buildSchema,
introspectionQuery,
GraphQLSchema,
Source,
buildClientSchema
} from "graphql";
import { execute, toPromise } from "apollo-link";
import { createHttpLink } from "apollo-link-http";

import { extractDocumentFromJavascript } from "apollo-codegen-core/lib/loading";
import { createHttpLink, HttpLink } from "apollo-link-http";
import { buildClientSchema, buildSchema, GraphQLSchema, introspectionQuery, Source } from "graphql";
import gql from "graphql-tag";
import { Agent, AgentOptions } from 'https';
import fetch from "node-fetch";
import * as path from "path";
import { URL } from 'url';
import { EndpointConfig } from "./config";
import { getIdFromKey, engineLink } from "./engine";
import { engineLink, getIdFromKey } from "./engine";
import { SCHEMA_QUERY } from "./operations/schema";

const introspection = gql(introspectionQuery);
Expand Down Expand Up @@ -55,16 +50,35 @@ export async function fromFile(
}

export const fetchSchema = async (
{ url, headers }: EndpointConfig,
{ url, headers, skipSSLValidation }: EndpointConfig,
projectFolder?: string
): Promise<GraphQLSchema | undefined> => {
if (!url) throw new Error("No endpoint provided when fetching schema");
const filePath = projectFolder ? path.resolve(projectFolder, url) : url;
if (fs.existsSync(filePath)) return fromFile(filePath);

var options: HttpLink.Options = { uri: url, fetch }

if (skipSSLValidation) {
const urlObject = new URL(url)
const host = urlObject.host
const port = +urlObject.port || 443

const agentOptions: AgentOptions = {
host: host,
port: port,
rejectUnauthorized: false
};

const agent = new Agent(agentOptions);

options.fetchOptions = { agent: agent }
}

return toPromise(
// XXX node-fetch isn't compatiable typescript wise here?
execute(createHttpLink({ uri: url, fetch } as any), {

// XXX node-fetch isn't compatible typescript wise here?
execute(createHttpLink(options), {
query: introspection,
context: { headers }
})
Expand Down
15 changes: 10 additions & 5 deletions packages/apollo-cli/src/load-config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {
loadConfigFromFile,
findAndLoadConfig,
SchemaDependency
} from "./config";
import { ListrTask } from "listr";
import { findAndLoadConfig, loadConfigFromFile, SchemaDependency } from "./config";

export function loadConfigStep(
flags: any,
Expand Down Expand Up @@ -87,6 +83,15 @@ export function loadConfigStep(
ctx.config.engineEndpoint = flags.engine;
}

if (flags.skipSSLValidation) {
if (Object.keys(ctx.config.schemas).length == 1) {
const endpointConfiguration = (Object.values(ctx.config.schemas)[0] as SchemaDependency).endpoint
if (endpointConfiguration) {
endpointConfiguration.skipSSLValidation = flags.skipSSLValidation
}
}
}

if (ctx.config.queries.length == 0 && ctx.config.schemas.default) {
ctx.config.queries.push({
schema: "default",
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-cli/src/load-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SchemaDependency, ApolloConfig } from "./config";
import { fetchSchema, fetchSchemaFromEngine } from "./fetch-schema";
import { GraphQLSchema } from "graphql";
import { ApolloConfig, SchemaDependency } from "./config";
import { fetchSchema, fetchSchemaFromEngine } from "./fetch-schema";

export async function loadSchema(
dependency: SchemaDependency,
Expand Down