hieunguyen30 commented on code in PR #518: URL: https://github.com/apache/airavata-custos/pull/518#discussion_r4464448492
########## connectors/LDAP/Provisioner/internal/operations/ensure_posix_account.go: ########## @@ -0,0 +1,401 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package operations + +import ( + "context" + "fmt" + "log/slog" + "regexp" + "strconv" + + "github.com/apache/airavata-custos/connectors/LDAP/Provisioner/internal/client" + "github.com/apache/airavata-custos/pkg/models" +) + +// identitySourcePrefix is the user_identities.source prefix for +// LDAP-assigned uidNumbers. The full source string is +// identitySourcePrefix + ":" + CustosClusterID so a deployment +// servicing multiple clusters caches each cluster's uidNumber +// separately — the same Custos user can have different POSIX uids +// on different clusters. Symmetric to the COmanage connector's +// source="comanage" tag but scoped per cluster to prevent the +// cross-cluster UID reuse that a flat source= would allow. +const identitySourcePrefix = "ldap" + +// identitySource returns the fully-qualified user_identities.source +// value for this connector instance's cluster. +func (o *Orchestrator) identitySource() string { + if id := o.c.Config().CustosClusterID; id != "" { + return identitySourcePrefix + ":" + id + } + return identitySourcePrefix +} + +// maxPosixUsernameLen is the traditional POSIX login-name cap. Matches +// what the pkg/posix allocator already enforces on the AMIE side. +const maxPosixUsernameLen = 32 + +// validPosixUsername matches a lowercase POSIX-conformant login name. +// Restricting to [a-z0-9_-] also happens to reject every RFC 4514 DN +// metacharacter (comma, plus, quote, backslash, less/greater, semicolon, +// equals, hash, space), so the RDN can be safely concatenated into +// uid=<value>,<BaseDN> without DN escaping. +var validPosixUsername = regexp.MustCompile(`^[a-z_][a-z0-9_-]*$`) + +// maxAllocRetries bounds how many times the orchestrator will re-allocate +// after a uidNumber constraint violation from an out-of-band writer that +// already holds our counter's next value. The persistent monotonic +// counter serialises Custos-side races itself (InnoDB row lock on the +// ldap_uid_sequence row), so retries are only reached against uids +// claimed by processes outside this connector. +const maxAllocRetries = 3 + +// ensurePOSIXAccountImpl provisions a posixAccount entry (and, when +// GroupBaseDN is configured, a matching posixGroup) for the given +// ComputeClusterUser. Mirrors the COmanage connector's shape: resolve +// the identity registry's assigned id, cache it in user_identities, +// materialise the POSIX record, and add the primary group. +// +// Direct-LDAP path specifics: +// - The identity registry is a persistent monotonic counter +// (internal/store.UIDSequence, one row per cluster in +// ldap_uid_sequence). Never regresses on entry deletion, so a new +// user cannot inherit a deleted user's numeric uid. +// - gidnumber = uidnumber (one-group-per-user), same pattern the +// COmanage flow uses on the CoGroup identifier. +// - user_identities(source="ldap:<clusterID>", external_id=<uidNumber>) +// caches the assignment so re-provisioning the same user is O(1). +// - When GroupBaseDN is set, a posixGroup with cn=<LocalUsername>, +// gidNumber=<uidNumber> is created too; without GroupBaseDN the +// connector assumes auto-private-groups on the client side. +func (o *Orchestrator) ensurePOSIXAccountImpl(ctx context.Context, cu *models.ComputeClusterUser) error { + log := slog.With( + "correlation_id", cu.ID, + "custos_user_id", cu.UserID, + "local_username", cu.LocalUsername, + "base_dn", o.c.Config().BaseDN, + ) + + if cu.LocalUsername == "" { + err := fmt.Errorf("compute_cluster_user %s has empty local_username", cu.ID) + o.dlq(ctx, cu, "validate_local_username", err) + return err + } + if len(cu.LocalUsername) > maxPosixUsernameLen || !validPosixUsername.MatchString(cu.LocalUsername) { + err := fmt.Errorf("compute_cluster_user %s has invalid local_username %q (must be POSIX-safe: lowercase, [a-z_][a-z0-9_-]*, <=%d chars)", + cu.ID, cu.LocalUsername, maxPosixUsernameLen) + o.dlq(ctx, cu, "validate_local_username", err) + return err + } + + user, err := o.core.GetUser(ctx, cu.UserID) + if err != nil { + o.dlq(ctx, cu, "get_custos_user", err) + return fmt.Errorf("get custos user %s: %w", cu.UserID, err) + } + if user == nil { + err := fmt.Errorf("custos user %s not found", cu.UserID) + o.dlq(ctx, cu, "get_custos_user", err) + return err + } + + // 1. Cache lookup — has this user already been assigned a uidNumber? + cached, err := o.findCachedUID(ctx, cu.UserID) + if err != nil { + o.dlq(ctx, cu, "list_user_identities", err) + return err + } + if cached > 0 { + log.Info("ldap provisioner: uidNumber cache hit", "uid", cached) + return o.ensureEntry(ctx, cu, user, cached) + } + + // 2. LDAP lookup — the entry may already exist (out-of-band or a + // prior run that failed to cache). Adopt its uidNumber. + adopted, err := o.tryAdoptExisting(ctx, cu, user) + if err != nil { + return err + } + if adopted { + return nil + } + + // 3. New user — pull a fresh uid from the persistent monotonic + // counter, then Add. The counter is a per-cluster row updated + // via one atomic UPDATE (InnoDB row lock), so allocations never + // regress even after LDAP entries are deleted and never collide + // with concurrent allocators. Constraint violations (from + // out-of-band writers or a mis-seeded counter) drive a bounded + // retry that pulls the next value. + cfg := o.c.Config() + for attempt := 1; attempt <= maxAllocRetries; attempt++ { + uid, allocErr := o.uids.Allocate(ctx, cfg.CustosClusterID) + if allocErr != nil { + o.dlq(ctx, cu, "allocate_uid", allocErr) + return allocErr + } + + acct := buildPosixAccount(cu, user, uid, cfg) + writtenDN, addErr := o.c.AddPosixAccount(acct) + if addErr == nil { + if cacheErr := o.storeUIDIdentity(ctx, cu.UserID, uid); cacheErr != nil { + log.Error("ldap provisioner: cache write failed after LDAP add; partial state, self-heals on next event", + "uid", uid, "err", cacheErr) + } + if err := o.ensurePrimaryGroup(ctx, cu, uid, log); err != nil { + o.dlq(ctx, cu, "ensure_primary_group", err) + return err + } + o.audit(ctx, cu, "LDAPAccountCreated", + fmt.Sprintf("dn=%s username=%s uid=%d gid=%d", writtenDN, cu.LocalUsername, uid, uid)) + log.Info("ldap provisioner: posixAccount created", "dn", writtenDN, "uid", uid) + return nil + } + if client.IsAlreadyExists(addErr) { + // Another Custos instance created the entry between our + // Find and Add. Adopt what's there rather than DLQ. + log.Info("ldap provisioner: entry created concurrently by another instance, adopting") + adopted, adoptErr := o.tryAdoptExisting(ctx, cu, user) + if adoptErr != nil { + return adoptErr + } + if adopted { + return nil + } + log.Warn("ldap provisioner: EntryAlreadyExists but Find empty, retrying", + "attempt", attempt) + continue + } + if !client.IsConstraintViolation(addErr) { + o.dlq(ctx, cu, "add_posix_account", addErr) + return addErr + } + // uidNumber uniqueness violation from an out-of-band writer + // (some entry in LDAP already has this number, e.g. a system + // user provisioned outside Custos). Pull the next value from + // the counter and try again. + log.Warn("ldap provisioner: uidNumber constraint violation, allocating next", + "attempt", attempt, "attempted_uid", uid) + } + + err = fmt.Errorf("failed to write posixAccount after %d retries", maxAllocRetries) + o.dlq(ctx, cu, "allocate_uid_retries_exhausted", err) + return err +} + +// tryAdoptExisting looks up a posixAccount by LocalUsername and, if +// present, caches its uidNumber and syncs mutable attributes via +// ensureEntry. Returns (adopted, err): +// +// - (true, nil) — an existing entry was adopted and fully processed +// (audit fired, group ensured); caller returns nil. +// - (false, nil) — no matching entry; caller falls through to +// fresh allocation. +// - (false, err) — an error occurred; DLQ has already been written. +// +// Used both by the initial LDAP-lookup path and by the retry loop when +// a concurrent instance's Add races ahead of ours. +func (o *Orchestrator) tryAdoptExisting(ctx context.Context, cu *models.ComputeClusterUser, user *models.User) (bool, error) { + dn, attrs, err := o.c.FindPosixAccount(cu.LocalUsername) Review Comment: I changed it so if theres no row and the username already exists in LDAP (account or group), it fails as a conflict with ErrPermanent and goes to failed deliveries. nothing gets adopted anymore -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
