|
| 1 | +package gitea |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "code.gitea.io/sdk/gitea" |
| 8 | + "github.com/CompassSecurity/pipeleak/helper" |
| 9 | + "github.com/rs/zerolog/log" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +func NewEnumCmd() *cobra.Command { |
| 14 | + enumCmd := &cobra.Command{ |
| 15 | + Use: "enum", |
| 16 | + Short: "Enumerate access rights of a Gitea access token", |
| 17 | + Long: "Enumerate access rights of a Gitea access token by retrieving the authenticated user's information.", |
| 18 | + Example: `pipeleak gitea enum --token $GITEA_TOKEN --gitea https://gitea.mycompany.com`, |
| 19 | + Run: Enum, |
| 20 | + } |
| 21 | + enumCmd.Flags().StringVarP(&giteaUrl, "gitea", "g", "https://gitea.com", "Gitea instance URL") |
| 22 | + enumCmd.Flags().StringVarP(&giteaApiToken, "token", "t", "", "Gitea API Token") |
| 23 | + |
| 24 | + enumCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose logging") |
| 25 | + return enumCmd |
| 26 | +} |
| 27 | + |
| 28 | +func Enum(cmd *cobra.Command, args []string) { |
| 29 | + helper.SetLogLevel(verbose) |
| 30 | + |
| 31 | + // Check if token is provided via flag or environment variable |
| 32 | + if giteaApiToken == "" { |
| 33 | + giteaApiToken = os.Getenv("GITEA_TOKEN") |
| 34 | + } |
| 35 | + |
| 36 | + if giteaApiToken == "" { |
| 37 | + log.Fatal().Msg("error: missing --token flag or GITEA_TOKEN environment variable") |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + // Initialize Gitea client |
| 42 | + client, err := gitea.NewClient(giteaUrl, gitea.SetToken(giteaApiToken)) |
| 43 | + if err != nil { |
| 44 | + log.Fatal().Stack().Err(err).Msg("Failed creating gitea client") |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + // Fetch user info |
| 49 | + log.Info().Msg("Enumerating User") |
| 50 | + user, _, err := client.GetMyUserInfo() |
| 51 | + if err != nil { |
| 52 | + log.Fatal().Stack().Err(err).Msg("Failed fetching current user") |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + // Log user data structure for debug visibility |
| 57 | + log.Debug().Interface("user", user).Msg("Full user data structure") |
| 58 | + |
| 59 | + // Output all user data fields in plain text |
| 60 | + fmt.Printf("\nAuthenticated User Information:\n") |
| 61 | + fmt.Printf("================================\n") |
| 62 | + fmt.Printf("ID: %d\n", user.ID) |
| 63 | + fmt.Printf("Username: %s\n", user.UserName) |
| 64 | + fmt.Printf("Login Name: %s\n", user.LoginName) |
| 65 | + fmt.Printf("Source ID: %d\n", user.SourceID) |
| 66 | + fmt.Printf("Full Name: %s\n", user.FullName) |
| 67 | + fmt.Printf("Email: %s\n", user.Email) |
| 68 | + fmt.Printf("Avatar URL: %s\n", user.AvatarURL) |
| 69 | + fmt.Printf("Language: %s\n", user.Language) |
| 70 | + fmt.Printf("Is Admin: %t\n", user.IsAdmin) |
| 71 | + fmt.Printf("Last Login: %s\n", user.LastLogin) |
| 72 | + fmt.Printf("Created: %s\n", user.Created) |
| 73 | + fmt.Printf("Restricted: %t\n", user.Restricted) |
| 74 | + fmt.Printf("Is Active: %t\n", user.IsActive) |
| 75 | + fmt.Printf("Prohibit Login: %t\n", user.ProhibitLogin) |
| 76 | + fmt.Printf("Location: %s\n", user.Location) |
| 77 | + fmt.Printf("Website: %s\n", user.Website) |
| 78 | + fmt.Printf("Description: %s\n", user.Description) |
| 79 | + fmt.Printf("Visibility: %s\n", user.Visibility) |
| 80 | + fmt.Printf("Followers: %d\n", user.FollowerCount) |
| 81 | + fmt.Printf("Following: %d\n", user.FollowingCount) |
| 82 | + fmt.Printf("Starred Repos: %d\n", user.StarredRepoCount) |
| 83 | + |
| 84 | + // Also log with structured logging |
| 85 | + log.Warn(). |
| 86 | + Int64("id", user.ID). |
| 87 | + Str("username", user.UserName). |
| 88 | + Str("fullName", user.FullName). |
| 89 | + Str("email", user.Email). |
| 90 | + Bool("isAdmin", user.IsAdmin). |
| 91 | + Bool("isActive", user.IsActive). |
| 92 | + Bool("restricted", user.Restricted). |
| 93 | + Msg("Current user") |
| 94 | + |
| 95 | + log.Info().Msg("Done") |
| 96 | +} |
0 commit comments