|
| 1 | +package transprocessor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/PythonHacker24/linux-acl-management-backend/internal/types" |
| 9 | + protos "github.com/PythonHacker24/linux-acl-management-backend/proto" |
| 10 | + "go.uber.org/zap" |
| 11 | +) |
| 12 | + |
| 13 | +/* takes a transactions and attempts to execute it via daemons */ |
| 14 | +func (p *PermProcessor) HandleRemoteTransaction(host string, port int, txn *types.Transaction, absolutePath string) error { |
| 15 | + |
| 16 | + /* if gRPCPool is nil, return an error */ |
| 17 | + if p.gRPCPool == nil { |
| 18 | + return fmt.Errorf("gRPC pool is nil") |
| 19 | + } |
| 20 | + |
| 21 | + /* get connection to the respective daemon */ |
| 22 | + address := fmt.Sprintf("%s:%d", host, port) |
| 23 | + conn, err := p.gRPCPool.GetConn(address, p.errCh) |
| 24 | + if err != nil { |
| 25 | + zap.L().Error("Failed to get connect with a daemon", |
| 26 | + zap.String("Address", address), |
| 27 | + zap.Error(err), |
| 28 | + ) |
| 29 | + } |
| 30 | + |
| 31 | + /* make it a for loop for interating all entries */ |
| 32 | + aclpayload := &protos.ACLEntry{ |
| 33 | + EntityType: txn.Entries.EntityType, |
| 34 | + Entity: txn.Entries.Entity, |
| 35 | + Permissions: txn.Entries.Permissions, |
| 36 | + Action: txn.Entries.Action, |
| 37 | + IsDefault: txn.Entries.IsDefault, |
| 38 | + } |
| 39 | + |
| 40 | + /* build the request for daemon */ |
| 41 | + request := &protos.ApplyACLRequest{ |
| 42 | + TransactionID: txn.ID.String(), |
| 43 | + TargetPath: absolutePath, |
| 44 | + Entry: aclpayload, |
| 45 | + } |
| 46 | + |
| 47 | + /* MAKE IT CONFIGURABLE */ |
| 48 | + ctx, _ := context.WithTimeout(context.Background(), 10 * time.Minute) |
| 49 | + |
| 50 | + aclClient := protos.NewACLServiceClient(conn) |
| 51 | + aclResponse, err := aclClient.ApplyACLEntry(ctx, request) |
| 52 | + if err != nil { |
| 53 | + p.errCh <- fmt.Errorf("failed to send ACL request to daemon") |
| 54 | + } |
| 55 | + |
| 56 | + if aclResponse.Success { |
| 57 | + /* set transaction successful*/ |
| 58 | + |
| 59 | + /* REMOVE THIS */ |
| 60 | + zap.L().Info("Transaction seems successful", |
| 61 | + zap.String("transaction ID", txn.ID.String()), |
| 62 | + ) |
| 63 | + |
| 64 | + txn.Output = "ACL executed successfully on filesystem servers" |
| 65 | + |
| 66 | + txn.ExecStatus = true |
| 67 | + } else { |
| 68 | + txn.ErrorMsg = "ACL failed to get executed in the filesystem server" |
| 69 | + } |
| 70 | + |
| 71 | + return nil |
| 72 | +} |
0 commit comments