A command-line tool to scan C# controller endpoints and update their authorization attributes. Migro streamlines managing role-based authorization by bridging a simple CSV file with your source code, offering both a scanner to discover endpoints and an updater to apply changes.
- ๐ Scan & Discover: Automatically scans a directory of C# controllers to find all HTTP endpoints.
- ๐ Template Generation: Generates a CSV template of all discovered methods, ready for you to define authorization rules.
- ๐ Smart Attribute Updates: Reads your edited CSV to add or replace
[Authorize]attributes in the source code, preserving indentation. - ๐น๏ธ Interactive Menu: A user-friendly interface guides you through scanning or updating.
- โก Flexible Update Modes: Choose between Interactive, Overwrite, and Preview modes when applying changes.
- ๐ Detailed Logging: Get comprehensive console output and a timestamped log file for every run.
- Go 1.21 or higher
- Go Figlet library:
go get github.com/common-n/go-figlet
git clone https://github.com/yourusername/migro.git
cd migro
go build -o migroDownload the latest release from the releases page.
Migro is an interactive tool. Simply run the executable, and it will guide you through the process.
./migroYou will be greeted with the main menu:
============================================================
C# Controller Authorize Attribute Scanner & Updater
============================================================
What would you like to do?
1. Scan Controllers to generate a CSV
2. Update Controllers from a CSV
3. Exit
Enter your choice (1-3):
This option discovers all HTTP endpoints and creates a CSV file for you to edit.
- Choose option
1from the main menu. - Enter the path to your controllers directory (e.g.,
./MyProject/Controllers). - Enter the desired path for the output CSV file (e.g.,
./mappings.csv). - The tool will scan the files and create the CSV.
This option applies the authorization rules from your edited CSV file to the source code.
- Choose option
2from the main menu. - Enter the path to your input CSV file.
- Enter the path to your controllers directory.
- Select an operation mode:
1. Interactive(Default): Prompts for confirmation before replacing any existing[Authorize]attribute or adding a new one. This is the safest option.2. Overwrite: Automatically applies all changes without asking for confirmation.3. Preview: Shows all changes that would be made without modifying any files. Highly recommended for a dry run.
- Scan: Run Migro and choose option
1to scan your project and generatemappings.csv. - Edit: Open
mappings.csvin a spreadsheet editor. Fill in theattributecolumn with the desired[Authorize]attributes for each method. - Preview: Run Migro again, choose option
2for updating, and then select thePreviewmode. Review the console output to ensure the changes are correct. - Apply: Once you are confident, run the updater again in
InteractiveorOverwritemode to apply the changes to your source code files. - Review: Check the changes in your version control system before committing.
The CSV file is the bridge between scanning and updating. The scanner generates this file, you edit it, and the updater consumes it. It requires the following columns:
| Column | Description | Example |
|---|---|---|
filename |
Controller file name | UserController.cs |
controller |
Controller class name (inferred from file) | UserController |
method |
Method name to update | GetTodoItems |
attribute |
The full [Authorize] attribute to apply |
[Authorize(Roles = "Administrator")] |
filename,controller,method,attribute
UserController.cs,UserController,GetTodoItems,"[Authorize(Roles = ""Administrator"")]"
UserController.cs,UserController,GetTodoItem,"[Authorize(Roles = ""Guest"")]"
UserController.cs,UserController,PutTodoItem,"[Authorize(Roles = ""User"")]"
TodoController.cs,TodoController,CreateTodo,"[Authorize(Roles = ""User,Administrator"")]"Note: In CSV, double quotes inside a quoted string must be escaped by doubling them (
"").
[Route("api/[controller]")]
[ApiController]
public class TodoItemsController : ControllerBase
{
[HttpGet]
public async Task<ActionResult<IEnumerable<TodoItemDTO>>> GetTodoItems()
{
// Method implementation
}
[Authorize] // Old generic authorization
[HttpGet("{id}")]
public async Task<ActionResult<TodoItemDTO>> GetTodoItem(long id)
{
// Method implementation
}
}[Route("api/[controller]")]
[ApiController]
public class TodoItemsController : ControllerBase
{
[Authorize(Roles = "Administrator")] // New role-specific authorization
[HttpGet]
public async Task<ActionResult<IEnumerable<TodoItemDTO>>> GetTodoItems()
{
// Method implementation
}
[Authorize(Roles = "Guest")] // Updated with specific role
[HttpGet("{id}")]
public async Task<ActionResult<TodoItemDTO>> GetTodoItem(long id)
{
// Method implementation
}
}- Recursively walks the specified directory to find all
.csfiles. - For each file, it reads the content and looks for method declarations.
- It checks if a method is an HTTP endpoint by looking for attributes like
[HttpGet],[HttpPost], etc., above it. - If it's an endpoint, it captures the method name and any existing
[Authorize]attribute. - Finally, it compiles this information into a CSV file.
- Parses the provided CSV file to load the mappings.
- For each row in the CSV, it opens the corresponding controller file.
- It finds the target method within the file.
- It analyzes the lines above the method to find the block of attributes and their indentation.
- Based on the selected mode (Interactive, Overwrite, Preview), it replaces or adds the new
[Authorize]attribute. - If not in preview mode, it writes the modified content back to the file.
- Always test with
Previewmode first to see changes before they are applied. - Use version control. Commit your code before running the updater.
- Validate your CSV format, especially the escaping of double quotes in attribute strings.
- Review the generated log file in the
logs/directory for any warnings or errors.
Method not found
- Ensure the method name in the CSV exactly matches the method in the C# file.
- Check for typos in filenames or method names.
CSV parsing errors
- Ensure proper quote escaping for attributes with string parameters:
"[Authorize(Roles = ""Admin"")]". - Check for missing columns or incomplete rows.
Permission denied
- Ensure the tool has write permissions to the controller files and log directory.
- Check if files are read-only or locked by another application.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/amazing-feature). - Commit your changes (
git commit -m 'Add some amazing feature'). - Push to the branch (
git push origin feature/amazing-feature). - Open a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
Note: This tool modifies your source code files. Always ensure you have proper backups and version control in place before running the tool on important codebases.