mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-06 13:27:22 +00:00
misc: added more error handling
This commit is contained in:
@@ -17,7 +17,7 @@ import {
|
|||||||
infisicalSymmetricDecrypt,
|
infisicalSymmetricDecrypt,
|
||||||
infisicalSymmetricEncypt
|
infisicalSymmetricEncypt
|
||||||
} from "@app/lib/crypto/encryption";
|
} from "@app/lib/crypto/encryption";
|
||||||
import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
|
import { BadRequestError, ForbiddenRequestError, NotFoundError, OidcAuthError } from "@app/lib/errors";
|
||||||
import { AuthMethod, AuthTokenType } from "@app/services/auth/auth-type";
|
import { AuthMethod, AuthTokenType } from "@app/services/auth/auth-type";
|
||||||
import { TAuthTokenServiceFactory } from "@app/services/auth-token/auth-token-service";
|
import { TAuthTokenServiceFactory } from "@app/services/auth-token/auth-token-service";
|
||||||
import { TokenType } from "@app/services/auth-token/auth-token-types";
|
import { TokenType } from "@app/services/auth-token/auth-token-types";
|
||||||
@@ -56,7 +56,7 @@ type TOidcConfigServiceFactoryDep = {
|
|||||||
orgBotDAL: Pick<TOrgBotDALFactory, "findOne" | "create" | "transaction">;
|
orgBotDAL: Pick<TOrgBotDALFactory, "findOne" | "create" | "transaction">;
|
||||||
licenseService: Pick<TLicenseServiceFactory, "getPlan" | "updateSubscriptionOrgMemberCount">;
|
licenseService: Pick<TLicenseServiceFactory, "getPlan" | "updateSubscriptionOrgMemberCount">;
|
||||||
tokenService: Pick<TAuthTokenServiceFactory, "createTokenForUser">;
|
tokenService: Pick<TAuthTokenServiceFactory, "createTokenForUser">;
|
||||||
smtpService: Pick<TSmtpService, "sendMail">;
|
smtpService: Pick<TSmtpService, "sendMail" | "verify">;
|
||||||
permissionService: Pick<TPermissionServiceFactory, "getOrgPermission">;
|
permissionService: Pick<TPermissionServiceFactory, "getOrgPermission">;
|
||||||
oidcConfigDAL: Pick<TOidcConfigDALFactory, "findOne" | "update" | "create">;
|
oidcConfigDAL: Pick<TOidcConfigDALFactory, "findOne" | "update" | "create">;
|
||||||
};
|
};
|
||||||
@@ -223,6 +223,7 @@ export const oidcConfigServiceFactory = ({
|
|||||||
let newUser: TUsers | undefined;
|
let newUser: TUsers | undefined;
|
||||||
|
|
||||||
if (serverCfg.trustOidcEmails) {
|
if (serverCfg.trustOidcEmails) {
|
||||||
|
// we prioritize getting the most complete user to create the new alias under
|
||||||
newUser = await userDAL.findOne(
|
newUser = await userDAL.findOne(
|
||||||
{
|
{
|
||||||
email,
|
email,
|
||||||
@@ -230,6 +231,16 @@ export const oidcConfigServiceFactory = ({
|
|||||||
},
|
},
|
||||||
tx
|
tx
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!newUser) {
|
||||||
|
// this fetches user entries created via invites
|
||||||
|
newUser = await userDAL.findOne(
|
||||||
|
{
|
||||||
|
username: email
|
||||||
|
},
|
||||||
|
tx
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!newUser) {
|
if (!newUser) {
|
||||||
@@ -332,14 +343,20 @@ export const oidcConfigServiceFactory = ({
|
|||||||
userId: user.id
|
userId: user.id
|
||||||
});
|
});
|
||||||
|
|
||||||
await smtpService.sendMail({
|
await smtpService
|
||||||
template: SmtpTemplates.EmailVerification,
|
.sendMail({
|
||||||
subjectLine: "Infisical confirmation code",
|
template: SmtpTemplates.EmailVerification,
|
||||||
recipients: [user.email],
|
subjectLine: "Infisical confirmation code",
|
||||||
substitutions: {
|
recipients: [user.email],
|
||||||
code: token
|
substitutions: {
|
||||||
}
|
code: token
|
||||||
});
|
}
|
||||||
|
})
|
||||||
|
.catch((err: Error) => {
|
||||||
|
throw new OidcAuthError({
|
||||||
|
message: `Error with SMTP configuration - contact the Infisical instance admin. ${err.message}`
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return { isUserCompleted, providerAuthToken };
|
return { isUserCompleted, providerAuthToken };
|
||||||
@@ -395,6 +412,17 @@ export const oidcConfigServiceFactory = ({
|
|||||||
message: `Organization bot for organization with ID '${org.id}' not found`,
|
message: `Organization bot for organization with ID '${org.id}' not found`,
|
||||||
name: "OrgBotNotFound"
|
name: "OrgBotNotFound"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const serverCfg = await getServerCfg();
|
||||||
|
if (isActive && !serverCfg.trustOidcEmails) {
|
||||||
|
const isSmtpConnected = await smtpService.verify();
|
||||||
|
if (!isSmtpConnected) {
|
||||||
|
throw new BadRequestError({
|
||||||
|
message: "Cannot enable OIDC when there are issues with the instance's SMTP configuration"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const key = infisicalSymmetricDecrypt({
|
const key = infisicalSymmetricDecrypt({
|
||||||
ciphertext: orgBot.encryptedSymmetricKey,
|
ciphertext: orgBot.encryptedSymmetricKey,
|
||||||
iv: orgBot.symmetricKeyIV,
|
iv: orgBot.symmetricKeyIV,
|
||||||
|
|||||||
@@ -133,3 +133,15 @@ export class ScimRequestError extends Error {
|
|||||||
this.status = status;
|
this.status = status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class OidcAuthError extends Error {
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
error: unknown;
|
||||||
|
|
||||||
|
constructor({ name, error, message }: { message?: string; name?: string; error?: unknown }) {
|
||||||
|
super(message || "Something went wrong");
|
||||||
|
this.name = name || "OidcAuthError";
|
||||||
|
this.error = error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ export const bootstrapCheck = async ({ db }: BootstrapOpt) => {
|
|||||||
.then(async () => {
|
.then(async () => {
|
||||||
console.info("SMTP successfully connected");
|
console.info("SMTP successfully connected");
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err: Error) => {
|
||||||
console.error(`SMTP - Failed to connect to ${appCfg.SMTP_HOST}:${appCfg.SMTP_PORT}`);
|
console.error(`SMTP - Failed to connect to ${appCfg.SMTP_HOST}:${appCfg.SMTP_PORT} - ${err.message}`);
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
GatewayTimeoutError,
|
GatewayTimeoutError,
|
||||||
InternalServerError,
|
InternalServerError,
|
||||||
NotFoundError,
|
NotFoundError,
|
||||||
|
OidcAuthError,
|
||||||
RateLimitError,
|
RateLimitError,
|
||||||
ScimRequestError,
|
ScimRequestError,
|
||||||
UnauthorizedError
|
UnauthorizedError
|
||||||
@@ -83,7 +84,10 @@ export const fastifyErrHandler = fastifyPlugin(async (server: FastifyZodProvider
|
|||||||
status: error.status,
|
status: error.status,
|
||||||
detail: error.detail
|
detail: error.detail
|
||||||
});
|
});
|
||||||
// Handle JWT errors and make them more human-readable for the end-user.
|
} else if (error instanceof OidcAuthError) {
|
||||||
|
void res
|
||||||
|
.status(HttpStatusCodes.InternalServerError)
|
||||||
|
.send({ statusCode: HttpStatusCodes.InternalServerError, message: error.message, error: error.name });
|
||||||
} else if (error instanceof jwt.JsonWebTokenError) {
|
} else if (error instanceof jwt.JsonWebTokenError) {
|
||||||
const message = (() => {
|
const message = (() => {
|
||||||
if (error.message === JWTErrors.JwtExpired) {
|
if (error.message === JWTErrors.JwtExpired) {
|
||||||
|
|||||||
@@ -77,5 +77,21 @@ export const smtpServiceFactory = (cfg: TSmtpConfig) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return { sendMail };
|
const verify = async () => {
|
||||||
|
const isConnected = smtp
|
||||||
|
.verify()
|
||||||
|
.then(async () => {
|
||||||
|
logger.info("SMTP connected");
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
.catch((err: Error) => {
|
||||||
|
logger.error("SMTP error");
|
||||||
|
logger.error(err);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
return isConnected;
|
||||||
|
};
|
||||||
|
|
||||||
|
return { sendMail, verify };
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user