Skip to content

Commit

Permalink
shcom
Browse files Browse the repository at this point in the history
  • Loading branch information
Kemitoxs committed Dec 6, 2023
1 parent cd86787 commit 8f8f201
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 169 deletions.
4 changes: 2 additions & 2 deletions Packrat/Fennec/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ await HttpContext.SignInAsync(
/// </remarks>
/// <returns></returns>
[Authorize]
[SwaggerResponse(StatusCodes.Status200OK, "The request is authenticated.")]
[SwaggerResponse(StatusCodes.Status401Unauthorized, "The request is not authenticated.")]
[SwaggerResponse(StatusCodes.Status204NoContent, "The request is authenticated")]
[SwaggerResponse(StatusCodes.Status401Unauthorized, "The request is not authenticated")]
[HttpGet("status")]
public IActionResult Status()
{
Expand Down
65 changes: 11 additions & 54 deletions Packrat/Fennec/Controllers/GraphController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public enum GroupType
Island
}

public record GroupRequest(GroupType GroupType, string Subnet, string SubnetMask);

public record GraphStatistics(long TotalHostCount, long TotalByteCount, long TotalPacketCount, long TotalTraceCount);

public record RequestStatistics(long NewHostCount, TimeSpan ProcessingTime);
Expand Down Expand Up @@ -61,35 +59,38 @@ public int GetHashCode(byte[]? obj)
}

[Authorize]
[Route("graph/{name}")]
[Route("graph/{layoutName}")]
[ApiController]
[Produces("application/json")]
[SwaggerTag("Generate Graphs")]
public class GraphController : ControllerBase
{
private readonly ITraceRepository _traceRepository;
private readonly ILayoutRepository _layoutRepository;

public GraphController(ITraceRepository traceRepository)
public GraphController(ITraceRepository traceRepository, ILayoutRepository layoutRepository)
{
_traceRepository = traceRepository;
_layoutRepository = layoutRepository;
}

/// <summary>
/// Generate the graph for a given layout within a specified timespan. Unknown nodes are added during this process.
/// Generate the graph for a given layout within a specified timespan.
/// </summary>
/// <remarks>
/// This endpoint fetches the graph layout, adds new nodes that were previously unknown, and returns the graph with its associated traces.
/// </remarks>
/// <param name="name">The name of the layout.</param>
/// <param name="layoutName">The name of the layout.</param>
/// <param name="request">A JSON body containing the timespan for which the graph is requested.</param>
/// <returns>An object containing the graph layout and its associated traces.</returns>
/// <response code="200">Successfully returned the graph layout.</response>
/// <response code="404">The layout specified by the name was not found.</response>
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GenerateGraph(string name, [FromBody] GraphRequest request)
public async Task<IActionResult> GenerateGraph(string layoutName, [FromBody] GraphRequest request)
{
var layout = await _layoutRepository.GetLayout(layoutName);
if (layout == null)
return NotFound("The given layout could not be found.");

var edges = await _traceRepository.AggregateTraces(request.From, request.To);

var nodes = edges
Expand Down Expand Up @@ -118,49 +119,5 @@ public async Task<IActionResult> GenerateGraph(string name, [FromBody] GraphRequ
dtoEdges);

return Ok(response);
}

/// <summary>
/// Groups nodes in a graph by the specified subnet and subnet mask.
/// </summary>
/// <remarks>
/// This endpoint allows you to create a new group within a layout based on a subnet and a subnet mask.
/// The nodes within the subnet will be grouped together and a new graph node will be created to represent them.
/// </remarks>
/// <param name="name">The name of the layout where the group will be created.</param>
/// <param name="request">A JSON body containing the subnet, subnet mask, and group type for the new group.</param>
/// <returns>Returns an object with details of the newly created group.</returns>
/// <response code="200">The group was successfully created.</response>
/// <response code="400">The subnet or subnet mask was invalid, or the group type is not supported.</response>
/// <response code="404">The given layout was not found.</response>
[HttpPost("group")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> CreateGroup(string name, [FromBody] GroupRequest request)
{
return Ok();
}


/// <summary>
/// Dissolves a group within a specific layout.
/// </summary>
/// <remarks>
/// This API removes a group node from a layout based on its ID and makes any corresponding host nodes visible.
/// </remarks>
/// <param name="name">The name of the layout.</param>
/// <param name="groupId">The ID of the group node to be dissolved.</param>
/// <returns>Returns Ok if the group is successfully dissolved.</returns>
/// <response code="200">The group was successfully dissolved.</response>
/// <response code="400">The given group node is not a compressed group.</response>
/// <response code="404">The given layout or group node was not found.</response>
[HttpDelete("group/{groupId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> DissolveGroup(string name, long groupId)
{
return Ok();
}
}
20 changes: 11 additions & 9 deletions Packrat/Fennec/Controllers/LayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Fennec.Controllers;
/// <param name="Type"></param>
/// <param name="Name"></param>
/// <param name="Description"></param>
public record ShortLayerDto(LayerType Type, string Name, bool Enabled, string Description);
public record ShortLayerDto(string Type, string Name, bool Enabled, string Description);

/// <summary>
/// Fully fledged layer with an arbitrary data object.
Expand All @@ -20,7 +20,7 @@ public record ShortLayerDto(LayerType Type, string Name, bool Enabled, string De
/// <param name="Name"></param>
/// <param name="Enabled"></param>
/// <param name="Data"></param>
public record FullLayerDto(LayerType Type, string Name, bool Enabled, object Data);
public record FullLayerDto(string Type, string Name, bool Enabled, ILayer Data);

/// <summary>
/// Provides direct manipulation capabilities to change the layers of a layout.
Expand All @@ -30,10 +30,12 @@ public record FullLayerDto(LayerType Type, string Name, bool Enabled, object Dat
public class LayerController : ControllerBase
{
private readonly ILayoutRepository _layouts;
private readonly ILayerRepository _layers;

Check failure on line 33 in Packrat/Fennec/Controllers/LayerController.cs

View workflow job for this annotation

GitHub Actions / build_and_test

The type or namespace name 'ILayerRepository' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 33 in Packrat/Fennec/Controllers/LayerController.cs

View workflow job for this annotation

GitHub Actions / build_and_test

The type or namespace name 'ILayerRepository' could not be found (are you missing a using directive or an assembly reference?)

public LayerController(ILayoutRepository layouts)
public LayerController(ILayoutRepository layouts, ILayerRepository layers)

Check failure on line 35 in Packrat/Fennec/Controllers/LayerController.cs

View workflow job for this annotation

GitHub Actions / build_and_test

The type or namespace name 'ILayerRepository' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 35 in Packrat/Fennec/Controllers/LayerController.cs

View workflow job for this annotation

GitHub Actions / build_and_test

The type or namespace name 'ILayerRepository' could not be found (are you missing a using directive or an assembly reference?)
{
_layouts = layouts;
_layers = layers;
}

/// <summary>
Expand All @@ -55,21 +57,19 @@ public async Task<IActionResult> InsertLayer(string layoutName, [FromQuery] int?
if (layout == null)
return NotFound("The given layout could not be found.");

ILayoutLayer? layer = creationRequest.Type switch
ILayer? layer = creationRequest.Type switch
{
LayerType.Filter => new FilterLayer(),
_ => null
};
if (layer == null)
return BadRequest("The given layer type is not supported.");

if (index == null)
layout.Layers.Add(layer);
else if (index < 0 || index > layout.Layers.Count)
if (index < 0 || index > layout.Layers.Count)
return BadRequest("The given index is out of bounds.");
else
layout.Layers.Insert(index.Value, layer);

await _layers.InsertLayer(layout, layer, index);
await _layouts.UpdateLayout(layout);
return Ok(layout);
}

Expand All @@ -79,6 +79,8 @@ public async Task<IActionResult> MoveLayer(string layoutName, int oldIndex, int
var layout = await _layouts.GetLayout(layoutName);
if (layout == null)
return NotFound("The given layout could not be found.");



return Ok();
}
Expand Down
103 changes: 76 additions & 27 deletions Packrat/Fennec/Controllers/LayoutController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using AutoMapper;
using Fennec.Database;
using Fennec.Database.Domain;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -7,6 +8,15 @@

namespace Fennec.Controllers;

/// <summary>
/// Represents a simplified layout with its layers removed.
/// </summary>
/// <param name="Name"></param>
/// <param name="LayerCount"></param>
public record ShortLayoutDto(string Name, int LayerCount);

public record FullLayoutDto(string Name, IList<ShortLayerDto> Layers);

/// <summary>
/// Create, update and delete <see cref="Layout" />s.
/// </summary>
Expand All @@ -17,75 +27,114 @@ namespace Fennec.Controllers;
[SwaggerTag("Manage Layouts")]
public class LayoutController : ControllerBase
{
private readonly IMapper _mapper;
private readonly ILayoutRepository _layoutRepository;

public LayoutController(ILayoutRepository layoutRepository)
public LayoutController(ILayoutRepository layoutRepository, IMapper mapper)
{
_layoutRepository = layoutRepository;
_mapper = mapper;
}

/// <summary>
/// List all layouts, ordered by name.
/// </summary>
/// <returns></returns>
[HttpGet]
[SwaggerOperation(Summary = "List all layouts, ordered by name")]
[SwaggerResponse(StatusCodes.Status200OK, "Layouts listed successfully")]
[SwaggerResponse(StatusCodes.Status200OK, "All layouts successfully returned", typeof(List<ShortLayoutDto>))]
public async Task<IActionResult> List()
{
var layouts = await _layoutRepository.GetLayouts();
return Ok(layouts);
var dtos = layouts.Select(l => _mapper.Map<ShortLayoutDto>(l));
return Ok(dtos);
}

/// <summary>
/// Create a new layout with the given <paramref name="name"/>.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[HttpPost("{name}")]
[SwaggerOperation(Summary = "Create a new layout")]
[SwaggerResponse(StatusCodes.Status201Created, "Layout created successfully")]
[SwaggerResponse(StatusCodes.Status400BadRequest, "A layout with the same name already exists")]
[SwaggerResponse(StatusCodes.Status201Created, "Layout successfully created", typeof(FullLayoutDto))]
[SwaggerResponse(StatusCodes.Status400BadRequest, "A layout with the name already exists")]
public async Task<IActionResult> Create(string name)
{
try
{
var layout = await _layoutRepository.CreateLayout(name);
return CreatedAtAction(nameof(Create), layout);
var dto = _mapper.Map<FullLayoutDto>(layout);
return CreatedAtAction(nameof(Create), dto);
}
catch (DuplicateNameException ex)
catch (DuplicateNameException)
{
return BadRequest(ex.Message);
return BadRequest($"A layout with the same name `{name}` already exists.");
}
}

[HttpPut("{name}")]
[SwaggerOperation(Summary = "Rename an existing layout")]
[SwaggerResponse(StatusCodes.Status200OK, "Layout renamed successfully")]
[SwaggerResponse(StatusCodes.Status404NotFound, "Layout not found")]
[SwaggerResponse(StatusCodes.Status400BadRequest, "A layout with the new name already exists")]
public async Task<IActionResult> Rename(string name, [FromQuery] string newName)
/// <summary>
/// Get a layout by name.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[HttpGet("{name}")]
[SwaggerResponse(StatusCodes.Status200OK, "Layout successfully returned", typeof(Layout))]
[SwaggerResponse(StatusCodes.Status404NotFound, "The layout with the name does not exist")]
public async Task<IActionResult> Get(string name)
{
var layout = await _layoutRepository.GetLayout(name);
if (layout == null)
return NotFound($"The layout with the name `{name}` does not exist.");

return Ok(layout);
}

/// <summary>
/// Rename an existing layout
/// </summary>
/// <param name="name"></param>
/// <param name="newName"></param>
/// <returns></returns>
[HttpPut("{name}/{newName}")]
[SwaggerResponse(StatusCodes.Status200OK, "Layout successfully renamed", typeof(FullLayoutDto))]
[SwaggerResponse(StatusCodes.Status404NotFound, "The layout with the name does not exist")]
[SwaggerResponse(StatusCodes.Status400BadRequest, "A layout with the name already exists")]
public async Task<IActionResult> Rename(string name, string newName)
{
try
{
var layout = await _layoutRepository.RenameLayout(name, newName);
return Ok(layout);
var dto = _mapper.Map<FullLayoutDto>(layout);
return Ok(dto);
}
catch (KeyNotFoundException ex)
catch (KeyNotFoundException)
{
return NotFound(ex.Message);
return NotFound($"The layout with the name `{name}` does not exist.");
}
catch (DuplicateNameException ex)
catch (DuplicateNameException)
{
return BadRequest(ex.Message);
return BadRequest($"A layout with the name `{name}` already exists.");
}
}

/// <summary>
/// Delete a layout
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[HttpDelete("{name}")]
[SwaggerOperation(Summary = "Delete a layout")]
[SwaggerResponse(StatusCodes.Status200OK, "Layout deleted successfully")]
[SwaggerResponse(StatusCodes.Status404NotFound, "Layout not found")]
[SwaggerResponse(StatusCodes.Status200OK, "Layout successfully deleted", typeof(FullLayoutDto))]
[SwaggerResponse(StatusCodes.Status404NotFound, "The layout with the name does not exist")]
public async Task<IActionResult> Delete(string name)
{
try
{
var layout = await _layoutRepository.DeleteLayout(name);
return Ok(layout);
var dto = _mapper.Map<FullLayoutDto>(layout);
return Ok(dto);
}
catch (KeyNotFoundException ex)
catch (KeyNotFoundException)
{
return NotFound(ex.Message);
return NotFound($"The layout with the name `{name}` does not exist.");
}
}
}
8 changes: 4 additions & 4 deletions Packrat/Fennec/Database/Domain/Layers/AggregationLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
/// <summary>
/// Groups nodes by their id.
/// </summary>
public class AggregationLayer : ILayoutLayer
public class AggregationLayer : ILayer
{
public LayerType Type { get; set; } = LayerType.Aggregation;
public string Type { get; set; } = LayerType.Aggregation;
public string? Name { get; set; }
public bool Enabled { get; set; }

public void ExecuteLayer()
public void ExecuteLayer(List<AggregateTrace> aggregateTraces)
{
throw new NotImplementedException();
}

public object GetPreview()
public string GetDescription()
{
throw new NotImplementedException();
}
Expand Down
Loading

0 comments on commit 8f8f201

Please sign in to comment.