Skip to content

Commit

Permalink
Merge branch 'main' into raphael/graphql-example
Browse files Browse the repository at this point in the history
  • Loading branch information
rderbier authored Oct 23, 2024
2 parents acba276 + fbb06da commit ac63793
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ In previous releases, the name "Hypermode" was used for all three._
- Improve dev first use log messages [#489](https://github.com/hypermodeinc/modus/pull/489)
- Highlight endpoints when running in dev [#490](https://github.com/hypermodeinc/modus/pull/490)
- Fix data race in logging adapter [#491](https://github.com/hypermodeinc/modus/pull/491)
- Move hyp settings for local model invocation to env variables [#495](https://github.com/hypermodeinc/modus/pull/495)
- Change GraphQL SDK examples to use a generic public GraphQL API [#501](https://github.com/hypermodeinc/modus/pull/501)

## 2024-10-02 - Version 0.12.7
Expand Down
17 changes: 13 additions & 4 deletions cli/src/commands/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as installer from "../../util/installer.js";
import { getHeader } from "../../custom/header.js";
import { getAppInfo } from "../../util/appinfo.js";
import { isOnline, withSpinner } from "../../util/index.js";
import { readSettingsJson } from "../../util/hypermode.js";
import BuildCommand from "../build/index.js";

export default class DevCommand extends Command {
Expand Down Expand Up @@ -142,12 +143,20 @@ export default class DevCommand extends Command {

await BuildCommand.run([appPath, "--no-logo"]);

// read from settings.json if it exists, load env vars into env
const settings = await readSettingsJson();

const env = {
...process.env,
MODUS_ENV: "dev",
HYP_EMAIL: settings.email,
HYP_JWT: settings.jwt,
HYP_ORG_ID: settings.orgId,
};

const runtime = spawn(runtimePath, ["-appPath", path.join(appPath, "build")], {
stdio: "inherit",
env: {
...process.env,
MODUS_ENV: "dev",
},
env: env,
});
runtime.on("close", (code) => this.exit(code || 1));

Expand Down
45 changes: 45 additions & 0 deletions cli/src/util/hypermode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2024 Hypermode Inc.
* Licensed under the terms of the Apache License, Version 2.0
* See the LICENSE file that accompanied this code for further details.
*
* SPDX-FileCopyrightText: 2024 Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/

import * as path from "node:path";
import * as fs from "node:fs";
import * as utils from "./fs.js";
import chalk from "chalk";

function getHypEnvDir(): string {
return path.join(process.env.HOME || "", ".hypermode");
}

function getSettingsFilePath(): string {
return path.join(getHypEnvDir(), "settings.json");
}

type HypSettings = {
email?: string;
jwt?: string;
orgId?: string;
};

export async function readSettingsJson(): Promise<HypSettings> {
const path = getSettingsFilePath();
const content = await utils.readFile(path, "utf-8");

const settings: HypSettings = {};

try {
const jsonContent = JSON.parse(content);
settings.email = jsonContent.HYP_EMAIL;
settings.jwt = jsonContent.HYP_JWT;
settings.orgId = jsonContent.HYP_ORG_ID;
} catch (e) {
console.warn(chalk.yellow("Error reading " + path), e);
}

return settings;
}
6 changes: 3 additions & 3 deletions runtime/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ require (
github.com/OneOfOne/xxhash v1.2.8
github.com/archdx/zerolog-sentry v1.8.4
github.com/aws/aws-sdk-go-v2 v1.32.2
github.com/aws/aws-sdk-go-v2/config v1.27.43
github.com/aws/aws-sdk-go-v2/service/s3 v1.65.3
github.com/aws/aws-sdk-go-v2/config v1.28.0
github.com/aws/aws-sdk-go-v2/service/s3 v1.66.0
github.com/aws/aws-sdk-go-v2/service/sts v1.32.2
github.com/buger/jsonparser v1.1.1
github.com/chewxy/math32 v1.11.1
Expand Down Expand Up @@ -47,7 +47,7 @@ require (
github.com/tidwall/gjson v1.18.0
github.com/tidwall/sjson v1.2.5
github.com/viterin/vek v0.4.2
github.com/wundergraph/graphql-go-tools/execution v1.0.6
github.com/wundergraph/graphql-go-tools/execution v1.0.7
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.105
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
google.golang.org/grpc v1.67.1
Expand Down
12 changes: 6 additions & 6 deletions runtime/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ github.com/aws/aws-sdk-go-v2 v1.32.2 h1:AkNLZEyYMLnx/Q/mSKkcMqwNFXMAvFto9bNsHqcT
github.com/aws/aws-sdk-go-v2 v1.32.2/go.mod h1:2SK5n0a2karNTv5tbP1SjsX0uhttou00v/HpXKM1ZUo=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.6 h1:pT3hpW0cOHRJx8Y0DfJUEQuqPild8jRGmSFmBgvydr0=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.6/go.mod h1:j/I2++U0xX+cr44QjHay4Cvxj6FUbnxrgmqN3H1jTZA=
github.com/aws/aws-sdk-go-v2/config v1.27.43 h1:p33fDDihFC390dhhuv8nOmX419wjOSDQRb+USt20RrU=
github.com/aws/aws-sdk-go-v2/config v1.27.43/go.mod h1:pYhbtvg1siOOg8h5an77rXle9tVG8T+BWLWAo7cOukc=
github.com/aws/aws-sdk-go-v2/config v1.28.0 h1:FosVYWcqEtWNxHn8gB/Vs6jOlNwSoyOCA/g/sxyySOQ=
github.com/aws/aws-sdk-go-v2/config v1.28.0/go.mod h1:pYhbtvg1siOOg8h5an77rXle9tVG8T+BWLWAo7cOukc=
github.com/aws/aws-sdk-go-v2/credentials v1.17.41 h1:7gXo+Axmp+R4Z+AK8YFQO0ZV3L0gizGINCOWxSLY9W8=
github.com/aws/aws-sdk-go-v2/credentials v1.17.41/go.mod h1:u4Eb8d3394YLubphT4jLEwN1rLNq2wFOlT6OuxFwPzU=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.17 h1:TMH3f/SCAWdNtXXVPPu5D6wrr4G5hI1rAxbcocKfC7Q=
Expand All @@ -39,8 +39,8 @@ github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.2 h1:s7NA1SOw8
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.2/go.mod h1:fnjjWyAW/Pj5HYOxl9LJqWtEwS7W2qgcRLWP+uWbss0=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.2 h1:t7iUP9+4wdc5lt3E41huP+GvQZJD38WLsgVp4iOtAjg=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.2/go.mod h1:/niFCtmuQNxqx9v8WAPq5qh7EH25U4BF6tjoyq9bObM=
github.com/aws/aws-sdk-go-v2/service/s3 v1.65.3 h1:xxHGZ+wUgZNACQmxtdvP5tgzfsxGS3vPpTP5Hy3iToE=
github.com/aws/aws-sdk-go-v2/service/s3 v1.65.3/go.mod h1:cB6oAuus7YXRZhWCc1wIwPywwZ1XwweNp2TVAEGYeB8=
github.com/aws/aws-sdk-go-v2/service/s3 v1.66.0 h1:xA6XhTF7PE89BCNHJbQi8VvPzcgMtmGC5dr8S8N7lHk=
github.com/aws/aws-sdk-go-v2/service/s3 v1.66.0/go.mod h1:cB6oAuus7YXRZhWCc1wIwPywwZ1XwweNp2TVAEGYeB8=
github.com/aws/aws-sdk-go-v2/service/sso v1.24.2 h1:bSYXVyUzoTHoKalBmwaZxs97HU9DWWI3ehHSAMa7xOk=
github.com/aws/aws-sdk-go-v2/service/sso v1.24.2/go.mod h1:skMqY7JElusiOUjMJMOv1jJsP7YUg7DrhgqZZWuzu1U=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.2 h1:AhmO1fHINP9vFYUE0LHzCWg/LfUWUF+zFPEcY9QXb7o=
Expand Down Expand Up @@ -260,8 +260,8 @@ github.com/wundergraph/cosmo/composition-go v0.0.0-20240926091419-7c3781f4f507 h
github.com/wundergraph/cosmo/composition-go v0.0.0-20240926091419-7c3781f4f507/go.mod h1:fUuOAUAXUFB/mlSkAaImGeE4A841AKR5dTMWhV4ibxI=
github.com/wundergraph/cosmo/router v0.0.0-20240926091419-7c3781f4f507 h1:uojstbUpR4s6uO3+PJCU/nJ8CT5rq1L7Q94jdf+OiG4=
github.com/wundergraph/cosmo/router v0.0.0-20240926091419-7c3781f4f507/go.mod h1:44kgNdnRaJVugapwUGRCbO27ziXNQTITJusRMmbl08c=
github.com/wundergraph/graphql-go-tools/execution v1.0.6 h1:Y8pzegICEhn7f6G94bjvZ1amUP6bdgHsImSkm/xuapA=
github.com/wundergraph/graphql-go-tools/execution v1.0.6/go.mod h1:sb+EyLG8bsKe8HVSU1tnTwWZSdhD8tFNvKH+1wllVuM=
github.com/wundergraph/graphql-go-tools/execution v1.0.7 h1:q+FYRoRjgj1Q6LowYlaxHKAe1YtJ0WlCchxqnBBMW9E=
github.com/wundergraph/graphql-go-tools/execution v1.0.7/go.mod h1:sb+EyLG8bsKe8HVSU1tnTwWZSdhD8tFNvKH+1wllVuM=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.105 h1:iSG8FONqEuujY6PQ5LvZplC8KjMLUbL1DEn6glepoT4=
github.com/wundergraph/graphql-go-tools/v2 v2.0.0-rc.105/go.mod h1:zkPVYJu1iQd0y1fBNj+oXe9uMI/33TSoiXEsKSAESZY=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
44 changes: 43 additions & 1 deletion runtime/secrets/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ import (
"net/http"
"os"
"regexp"
"time"

"github.com/fatih/color"
"github.com/golang-jwt/jwt/v5"
"github.com/hypermodeinc/modus/lib/manifest"
"github.com/hypermodeinc/modus/runtime/logger"
"github.com/hypermodeinc/modus/runtime/utils"
)

var provider secretsProvider
var errLocalAuthFailed = fmt.Errorf("local authentication failed")

type secretsProvider interface {
initialize(ctx context.Context)
Expand Down Expand Up @@ -93,8 +97,24 @@ func ApplyAuthToLocalHypermodeModelRequest(ctx context.Context, connection manif
jwt := os.Getenv("HYP_JWT")
orgId := os.Getenv("HYP_ORG_ID")

warningColor := color.New(color.FgHiYellow, color.Bold)

if jwt == "" || orgId == "" {
return fmt.Errorf("missing HYP_JWT or HYP_ORG_ID environment variables, login to Hypermode using 'hyp login'")
fmt.Println()
warningColor.Println("Warning: Local authentication not found. Please login using `hyp login`")
fmt.Println()
return errLocalAuthFailed
}

isExpired, err := checkJWTExpiration(jwt)
if err != nil {
return err
}
if isExpired {
fmt.Println()
warningColor.Println("Warning: Local authentication expired. Please login using `hyp login`")
fmt.Println()
return errLocalAuthFailed
}

req.Header.Set("Authorization", "Bearer "+jwt)
Expand All @@ -103,6 +123,28 @@ func ApplyAuthToLocalHypermodeModelRequest(ctx context.Context, connection manif
return nil
}

// checkJWTExpiration checks if the JWT has expired based on the 'exp' claim.
func checkJWTExpiration(tokenString string) (bool, error) {
p := jwt.Parser{}
token, _, err := p.ParseUnverified(tokenString, jwt.MapClaims{})
if err != nil {
return false, fmt.Errorf("failed to parse: %w", err)
}

claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return false, fmt.Errorf("failed to extract claims from JWT")
}

exp, ok := claims["exp"].(float64)
if !ok {
return false, fmt.Errorf("exp claim is missing or not a number")
}

expirationTime := time.Unix(int64(exp), 0)
return time.Now().After(expirationTime), nil
}

// ApplySecretsToString evaluates the given string and replaces any placeholders
// present in the string with their secret values for the given connection.
func ApplySecretsToString(ctx context.Context, connection manifest.ConnectionInfo, str string) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion sdk/assemblyscript/examples/collections/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Modus Collections Example

This example demonstrates the capabilities of Hypermode collections feature.
This example demonstrates the capabilities of Modus's collections feature.

See [./assembly/index.ts](./assembly/index.ts) for the implementation.
9 changes: 4 additions & 5 deletions sdk/assemblyscript/src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/assemblyscript/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"devDependencies": {
"@eslint/js": "^9.13.0",
"@types/eslint__js": "^8.42.3",
"@types/node": "^22.7.7",
"@types/node": "^22.7.9",
"as-test": "^0.3.5",
"assemblyscript": "^0.27.30",
"assemblyscript-prettier": "^3.0.1",
Expand Down
6 changes: 3 additions & 3 deletions sdk/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/hypermodeinc/modus/sdk/go
go 1.23.0

require (
github.com/hypermodeinc/modus/lib/manifest v0.13.0-alpha1
github.com/hypermodeinc/modus/lib/wasmextractor v0.13.0-alpha1
github.com/hypermodeinc/modus/lib/manifest v0.13.0-alpha2
github.com/hypermodeinc/modus/lib/wasmextractor v0.13.0-alpha2
)

// NOTE: during developement, you can use replace directives if needed
Expand All @@ -15,7 +15,7 @@ require (
// replace github.com/hypermodeinc/modus/lib/wasmextractor => ../../lib/wasmextractor

require (
github.com/fatih/color v1.17.0
github.com/fatih/color v1.18.0
github.com/hashicorp/go-version v1.7.0
github.com/rs/xid v1.6.0
github.com/tidwall/sjson v1.2.5
Expand Down
12 changes: 6 additions & 6 deletions sdk/go/go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hypermodeinc/modus/lib/manifest v0.13.0-alpha1 h1:zd0f6361EIDovowOWuKycyJAhcJrecpZJxMWjRx3TmQ=
github.com/hypermodeinc/modus/lib/manifest v0.13.0-alpha1/go.mod h1:ymRlTZWerFnIUVpvEonTMTo38EDYzHcGSNYTOv2MITk=
github.com/hypermodeinc/modus/lib/wasmextractor v0.13.0-alpha1 h1:NClXj9rPg9nAnwrXgH9Gf/xLuPtxveC+plkKb68ux48=
github.com/hypermodeinc/modus/lib/wasmextractor v0.13.0-alpha1/go.mod h1:YCesMU95vF5qkscLMKSYr92OloLe1KGwyiqW2i4OmnE=
github.com/hypermodeinc/modus/lib/manifest v0.13.0-alpha2 h1:SD3Tnzp7zCYoT8c87VC0rqlIQJuyihEI9uUft99Hb4I=
github.com/hypermodeinc/modus/lib/manifest v0.13.0-alpha2/go.mod h1:ymRlTZWerFnIUVpvEonTMTo38EDYzHcGSNYTOv2MITk=
github.com/hypermodeinc/modus/lib/wasmextractor v0.13.0-alpha2 h1:Wn+RRT05S4zbSNmLv774V5Px8uAmVZtB/x50e5Z0Fms=
github.com/hypermodeinc/modus/lib/wasmextractor v0.13.0-alpha2/go.mod h1:YCesMU95vF5qkscLMKSYr92OloLe1KGwyiqW2i4OmnE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down

0 comments on commit ac63793

Please sign in to comment.