Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

add auth login failed on identity ua service

adilson60009e9ca10d
diff --git a/backend/src/ee/services/audit-log/audit-log-service.ts b/backend/src/ee/services/audit-log/audit-log-service.ts
index 7b0870243e..2b64e0d4e3 100644
--- a/backend/src/ee/services/audit-log/audit-log-service.ts
+++ b/backend/src/ee/services/audit-log/audit-log-service.ts
@@ -110,7 +110,10 @@ export const auditLogServiceFactory = ({
return;
}
// add all cases in which project id or org id cannot be added
- if (data.event.type !== EventType.LOGIN_IDENTITY_UNIVERSAL_AUTH) {
+ if (
+ data.event.type !== EventType.LOGIN_IDENTITY_UNIVERSAL_AUTH &&
+ data.event.type !== EventType.LOGIN_IDENTITY_UNIVERSAL_AUTH_FAILED
+ ) {
if (!data.projectId && !data.orgId)
throw new BadRequestError({ message: "Must specify either project id or org id" });
}
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.ts
index a8f57fda40..d2fd2af17e 100644
--- a/backend/src/ee/services/audit-log/audit-log-types.ts
+++ b/backend/src/ee/services/audit-log/audit-log-types.ts
@@ -191,6 +191,7 @@ export enum EventType {
MACHINE_IDENTITY_AUTH_TEMPLATE_UPDATE = "machine-identity-auth-template-update",
MACHINE_IDENTITY_AUTH_TEMPLATE_DELETE = "machine-identity-auth-template-delete",
LOGIN_IDENTITY_UNIVERSAL_AUTH = "login-identity-universal-auth",
+ LOGIN_IDENTITY_UA_AUTH_FAILED = "login-identity-ua-auth-failed",
ADD_IDENTITY_UNIVERSAL_AUTH = "add-identity-universal-auth",
UPDATE_IDENTITY_UNIVERSAL_AUTH = "update-identity-universal-auth",
GET_IDENTITY_UNIVERSAL_AUTH = "get-identity-universal-auth",
@@ -1149,6 +1150,15 @@ interface LoginIdentityUniversalAuthEvent {
};
}
+interface LoginIdentityUniversalAuthFailedEvent {
+ type: EventType.LOGIN_IDENTITY_UA_AUTH_FAILED;
+ metadata: {
+ clientId: string;
+ identityId: string | null;
+ reason: string;
+ };
+}
+
interface MachineIdentityAuthTemplateCreateEvent {
type: EventType.MACHINE_IDENTITY_AUTH_TEMPLATE_CREATE;
metadata: {
@@ -5487,6 +5497,7 @@ export type Event =
| UpdateIdentityEvent
| DeleteIdentityEvent
| LoginIdentityUniversalAuthEvent
+ | LoginIdentityUniversalAuthFailedEvent
| MachineIdentityAuthTemplateCreateEvent
| MachineIdentityAuthTemplateUpdateEvent
| MachineIdentityAuthTemplateDeleteEvent
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index 7aabdfd7bb..edb1f22105 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -1873,7 +1873,8 @@ export const registerRoutes = async (
licenseService,
keyStore,
orgDAL,
- membershipIdentityDAL
+ membershipIdentityDAL,
+ auditLogService
});
const identityKubernetesAuthService = identityKubernetesAuthServiceFactory({
diff --git a/backend/src/server/routes/v1/identity-universal-auth-router.ts b/backend/src/server/routes/v1/identity-universal-auth-router.ts
index 73f85d2e98..25c8ae80fe 100644
--- a/backend/src/server/routes/v1/identity-universal-auth-router.ts
+++ b/backend/src/server/routes/v1/identity-universal-auth-router.ts
@@ -63,7 +63,9 @@ export const registerIdentityUaRouter = async (server: FastifyZodProvider) => {
accessTokenMaxTTL
} = await server.services.identityUa.login({
...req.body,
- ip: req.realIp
+ ip: req.realIp,
+ userAgent: req.auditLogInfo.userAgent,
+ userAgentType: req.auditLogInfo.userAgentType
});
await server.services.auditLog.createAuditLog({
diff --git a/backend/src/services/identity-ua/identity-ua-service.ts b/backend/src/services/identity-ua/identity-ua-service.ts
index f075087e0b..34cff06600 100644
--- a/backend/src/services/identity-ua/identity-ua-service.ts
+++ b/backend/src/services/identity-ua/identity-ua-service.ts
@@ -2,6 +2,7 @@ import { ForbiddenError, subject } from "@casl/ability";
import { requestContext } from "@fastify/request-context";
import { AccessScope, ActionProjectType, IdentityAuthMethod, OrganizationActionScope } from "@app/db/schemas";
+import { EventType, TAuditLogServiceFactory } from "@app/ee/services/audit-log/audit-log-types";
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import { OrgPermissionIdentityActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
import {
@@ -44,6 +45,7 @@ import {
TLoginUaDTO,
TRevokeUaClientSecretDTO,
TRevokeUaDTO,
+ TThrowUnauthorizedWithAuditLogDTO,
TUpdateUaDTO
} from "./identity-ua-types";
@@ -60,6 +62,7 @@ type TIdentityUaServiceFactoryDep = {
TKeyStoreFactory,
"setItemWithExpiry" | "getItem" | "deleteItem" | "getKeysByPattern" | "deleteItems" | "acquireLock"
>;
+ auditLogService: Pick<TAuditLogServiceFactory, "createAuditLog">;
};
export type TIdentityUaServiceFactory = ReturnType<typeof identityUaServiceFactory>;
@@ -78,14 +81,45 @@ export const identityUaServiceFactory = ({
licenseService,
orgDAL,
keyStore,
- identityDAL
+ identityDAL,
+ auditLogService
}: TIdentityUaServiceFactoryDep) => {
- const login = async ({ clientId, clientSecret, ip, organizationSlug }: TLoginUaDTO) => {
+ const throwUnauthorizedWithAuditLog = async ({
+ message,
+ clientId,
+ identityId,
+ reason,
+ orgId,
+ ip,
+ userAgent,
+ userAgentType
+ }: TThrowUnauthorizedWithAuditLogDTO): Promise<never> => {
+ await auditLogService.createAuditLog({
+ actor: { type: ActorType.UNKNOWN_USER, metadata: {} },
+ orgId,
+ ipAddress: ip,
+ userAgent,
+ userAgentType,
+ event: {
+ type: EventType.LOGIN_IDENTITY_UA_AUTH_FAILED,
+ metadata: { clientId, identityId, reason }
+ }
+ });
+ throw new UnauthorizedError({ message });
+ };
+
+ const login = async ({ clientId, clientSecret, ip, organizationSlug, userAgent, userAgentType }: TLoginUaDTO) => {
const appCfg = getConfig();
const identityUa = await identityUaDAL.findOne({ clientId });
if (!identityUa) {
- throw new UnauthorizedError({
- message: "Invalid credentials"
+ await throwUnauthorizedWithAuditLog({
+ message: "Invalid credentials",
+ clientId,
+ identityId: null,
+ reason: "client_id_not_found",
+ ip,
+ userAgent,
+ userAgentType
});
}
@@ -112,8 +146,15 @@ export const identityUaServiceFactory = ({
}
if (lockout && lockout.lockedOut) {
- throw new UnauthorizedError({
- message: "This identity auth method is temporarily locked, please try again later"
+ await throwUnauthorizedWithAuditLog({
+ message: "This identity auth method is temporarily locked, please try again later",
+ clientId,
+ identityId: identityUa.identityId,
+ reason: "temporarily_locked",
+ orgId: identity.orgId,
+ ip,
+ userAgent,
+ userAgentType
});
}
@@ -156,8 +197,15 @@ export const identityUaServiceFactory = ({
}
if (lockout.lockedOut) {
- throw new UnauthorizedError({
- message: "This identity auth method is temporarily locked, please try again later"
+ await throwUnauthorizedWithAuditLog({
+ message: "This identity auth method is temporarily locked, please try again later",
+ clientId,
+ identityId: identityUa.identityId,
+ reason: "temporarily_locked",
+ orgId: identity.orgId,
+ ip,
+ userAgent,
+ userAgentType
});
}
@@ -186,7 +234,16 @@ export const identityUaServiceFactory = ({
}
}
- throw new UnauthorizedError({ message: "Invalid credentials" });
+ await throwUnauthorizedWithAuditLog({
+ message: "Invalid credentials",
+ clientId,
+ identityId: identityUa.identityId,
+ reason: "invalid_client_secret",
+ orgId: identity.orgId,
+ ip,
+ userAgent,
+ userAgentType
+ });
} else if (lockout) {
// If credentials are valid, clear any existing lockout record
await keyStore.deleteItem(LOCKOUT_KEY);
@@ -204,8 +261,15 @@ export const identityUaServiceFactory = ({
isClientSecretRevoked: true
});
- throw new UnauthorizedError({
- message: "Access denied due to expired client secret"
+ await throwUnauthorizedWithAuditLog({
+ message: "Access denied due to expired client secret",
+ clientId,
+ identityId: identityUa.identityId,
+ reason: "client_secret_expired",
+ orgId: identity.orgId,
+ ip,
+ userAgent,
+ userAgentType
});
}
}
@@ -216,8 +280,15 @@ export const identityUaServiceFactory = ({
await identityUaClientSecretDAL.updateById(validClientSecretInfo.id, {
isClientSecretRevoked: true
});
- throw new UnauthorizedError({
- message: "Access denied due to client secret usage limit reached"
+ await throwUnauthorizedWithAuditLog({
+ message: "Access denied due to client secret usage limit reached",
+ clientId,
+ identityId: identityUa.identityId,
+ reason: "client_secret_usage_limit_reached",
+ orgId: identity.orgId,
+ ip,
+ userAgent,
+ userAgentType
});
}
@@ -250,8 +321,15 @@ export const identityUaServiceFactory = ({
});
if (!subOrgMembership) {
- throw new UnauthorizedError({
- message: `Identity not authorized to access sub organization ${organizationSlug}`
+ await throwUnauthorizedWithAuditLog({
+ message: `Identity not authorized to access sub organization ${organizationSlug}`,
+ clientId,
+ identityId: identityUa.identityId,
+ reason: "sub_org_not_authorized",
+ orgId: identity.orgId,
+ ip,
+ userAgent,
+ userAgentType
});
}
diff --git a/backend/src/services/identity-ua/identity-ua-types.ts b/backend/src/services/identity-ua/identity-ua-types.ts
index 138690ca41..8ed65c756f 100644
--- a/backend/src/services/identity-ua/identity-ua-types.ts
+++ b/backend/src/services/identity-ua/identity-ua-types.ts
@@ -1,3 +1,4 @@
+import { UserAgentType } from "@app/ee/services/audit-log/audit-log-types";
import { TProjectPermission } from "@app/lib/types";
export type TLoginUaDTO = {
@@ -5,6 +6,8 @@ export type TLoginUaDTO = {
clientSecret: string;
ip: string;
organizationSlug?: string;
+ userAgent?: string;
+ userAgentType?: UserAgentType;
};
export type TAttachUaDTO = {
@@ -68,3 +71,14 @@ export type TGetUniversalAuthClientSecretByIdDTO = {
identityId: string;
clientSecretId: string;
} & Omit<TProjectPermission, "projectId">;
+
+export type TThrowUnauthorizedWithAuditLogDTO = {
+ message: string;
+ clientId: string;
+ identityId: string | null;
+ reason: string;
+ orgId?: string;
+ ip?: string;
+ userAgent?: string;
+ userAgentType?: UserAgentType;
+};