demo_cli.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "igit.com/xbase/raft"
  6. )
  7. // DemoAuthCmds extends the standard CLI with specific auth testing scenarios
  8. func RegisterDemoAuthCommands(cli *raft.CLI) {
  9. // 1. Scenario: Initialize Sales System
  10. cli.RegisterCommand("demo-init", "Setup roles and users for demo", func(parts []string, server *raft.KVServer) {
  11. // This must be run by root
  12. // Create Role: Sales Manager (0.5 - 0.9 discount)
  13. managerRole := raft.Role{
  14. Name: "sales_manager",
  15. Permissions: []raft.Permission{
  16. {
  17. KeyPattern: "product.discount",
  18. Actions: []string{"write", "read"},
  19. Constraint: &raft.Constraint{Min: floatPtr(0.5), Max: floatPtr(0.9)},
  20. },
  21. {
  22. KeyPattern: "product.info.*",
  23. Actions: []string{"write", "read"},
  24. },
  25. },
  26. }
  27. // Create Role: Junior Sales (0.8 - 0.95 discount)
  28. juniorRole := raft.Role{
  29. Name: "junior_sales",
  30. Permissions: []raft.Permission{
  31. {
  32. KeyPattern: "product.discount",
  33. Actions: []string{"write", "read"},
  34. Constraint: &raft.Constraint{Min: floatPtr(0.8), Max: floatPtr(0.95)},
  35. },
  36. },
  37. }
  38. // Save Roles
  39. role1JSON, _ := json.Marshal(managerRole)
  40. role2JSON, _ := json.Marshal(juniorRole)
  41. // We use SetAuthenticated with current token (should be root)
  42. // Or if auth not enabled yet, regular Set works.
  43. // Assuming we are logged in as root.
  44. // Use internal Set for demo setup convenience (bypassing CLI token check if we want, but better to follow rules)
  45. // Let's assume the user ran 'auth-init' and 'login root' before this.
  46. // We will try to use the token from CLI if available.
  47. fmt.Println("Creating roles...")
  48. // Direct Set for demo purposes if auth is not strictly enforced yet or we are root
  49. if err := server.Set("system.role.sales_manager", string(role1JSON)); err != nil {
  50. fmt.Printf("Error creating manager role: %v\n", err)
  51. }
  52. if err := server.Set("system.role.junior_sales", string(role2JSON)); err != nil {
  53. fmt.Printf("Error creating junior role: %v\n", err)
  54. }
  55. // Create Users
  56. fmt.Println("Creating users: 'alice' (Manager) and 'bob' (Junior)...")
  57. alice := raft.User{
  58. Username: "alice",
  59. Salt: "salt1",
  60. PasswordHash: hashPassword("pass123", "salt1"), // Helper needed or manual hash
  61. Roles: []string{"sales_manager"},
  62. }
  63. bob := raft.User{
  64. Username: "bob",
  65. Salt: "salt2",
  66. PasswordHash: hashPassword("pass123", "salt2"),
  67. Roles: []string{"junior_sales"},
  68. }
  69. user1JSON, _ := json.Marshal(alice)
  70. user2JSON, _ := json.Marshal(bob)
  71. server.Set("system.user.alice", string(user1JSON))
  72. server.Set("system.user.bob", string(user2JSON))
  73. fmt.Println("Demo environment initialized!")
  74. fmt.Println("Try: login alice pass123")
  75. fmt.Println("Then: set product.discount 0.6 (Should succeed)")
  76. fmt.Println("Then: set product.discount 0.1 (Should fail)")
  77. })
  78. }
  79. // Helpers duplicated from auth.go for demo setup (since they are private there)
  80. // In a real app we would export them or use the API
  81. func hashPassword(password, salt string) string {
  82. return raft.HashPassword(password, salt)
  83. }
  84. func floatPtr(v float64) *float64 {
  85. return &v
  86. }