Skip to content

Commit

Permalink
Upgrade .NET from 6.0 to 8.0 (#3046)
Browse files Browse the repository at this point in the history
Co-authored-by: Jim Grady <[email protected]>
  • Loading branch information
imnasnainaec and jmgrady authored Jun 14, 2024
1 parent 8ca63d9 commit b4d3809
Show file tree
Hide file tree
Showing 26 changed files with 352 additions and 260 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ indent_size = 4
# CA1305 requires using a FormatProvider with int.Parse and string.Format.
dotnet_diagnostic.CA1305.severity = none
dotnet_diagnostic.CA1710.severity = warning
# CA1825 doesn't like `Produces("application/json")` in our controllers.
dotnet_diagnostic.CA1825.severity = none
# TODO: Implement LoggerMessage pattern to remove the CA1848 exception.
dotnet_diagnostic.CA1848.severity = none
# CS1591 is our only exception to EnforceCodeStyleInBuild+GenerateDocumentationFile.
Expand Down
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DOTNET_WATCH_RESTART_ON_RUDE_EDIT=true
9 changes: 5 additions & 4 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
dotnet: ["6.0.x"]
dotnet: ["8.0.x"]
steps:
# See https://docs.stepsecurity.io/harden-runner/getting-started/ for instructions on
# configuring harden-runner and identifying allowed endpoints.
Expand Down Expand Up @@ -114,14 +114,14 @@ jobs:
disable-sudo: true
egress-policy: block
allowed-endpoints: >
*.actions.githubusercontent.com:443
aka.ms:443
api.github.com:443
api.nuget.org:443
dc.services.visualstudio.com:443
dotnetcli.azureedge.net:443
github.com:443
objects.githubusercontent.com:443
ts-crl.ws.symantec.com:80
- name: Checkout repository
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
Expand All @@ -130,7 +130,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@4d6c8fcf3c8f7a60068d26b594648e99df24cee3 # v4.0.0
with:
dotnet-version: "6.0.x"
dotnet-version: "8.0.x"
- name: Initialize CodeQL
uses: github/codeql-action/init@d39d31e687223d841ef683f52467bd88e9b21c14 # v3.25.3
with:
Expand Down Expand Up @@ -159,15 +159,16 @@ jobs:
disable-file-monitoring: true
egress-policy: block
allowed-endpoints: >
*.actions.githubusercontent.com:443
*.data.mcr.microsoft.com:443
*.symcb.com:80
api.nuget.org:443
archive.ubuntu.com:80
dc.services.visualstudio.com:443
deb.debian.org:80
github.com:443
mcr.microsoft.com:443
security.ubuntu.com:80
ts-crl.ws.symantec.com:80
# For subfolders, currently a full checkout is required.
# See: https://github.com/marketplace/actions/build-and-push-docker-images#path-context
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ build
# Generated files for development use
docker-compose.yml
.env.*
!.env.development
.env*.local
nginx/scripts
/backups
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Backend/bin/Debug/net6.0/BackendFramework.dll",
"program": "${workspaceFolder}/Backend/bin/Debug/net8.0/BackendFramework.dll",
"args": [],
"cwd": "${workspaceFolder}/Backend",
"stopAtEntry": false,
Expand Down
4 changes: 2 additions & 2 deletions Backend.Tests/Backend.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<CollectCoverage>true</CollectCoverage>
Expand All @@ -9,7 +9,7 @@
<AnalysisMode>Recommended</AnalysisMode>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CA1305;CS1591</NoWarn>
<NoWarn>$(NoWarn);CA1305;CA1859;CS1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
Expand Down
2 changes: 2 additions & 0 deletions Backend.Tests/Controllers/LiftControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,9 @@ public void TestRoundtrip(RoundTripObj roundTripObj)

private sealed class MockLogger : ILogger<LiftController>
{
#pragma warning disable CS8633
public IDisposable BeginScope<TState>(TState state)
#pragma warning restore CS8633
{
throw new NotImplementedException();
}
Expand Down
7 changes: 1 addition & 6 deletions Backend.Tests/Mocks/PermissionServiceMock.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using BackendFramework.Interfaces;
using BackendFramework.Models;
Expand Down Expand Up @@ -156,12 +155,8 @@ public string GetUserId(HttpContext? request)
}
}

[Serializable]
internal class UserAuthenticationException : Exception
internal sealed class UserAuthenticationException : Exception
{
public UserAuthenticationException() { }

protected UserAuthenticationException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
}
}
14 changes: 5 additions & 9 deletions Backend.Tests/Mocks/UserRepositoryMock.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using BackendFramework.Helper;
using BackendFramework.Interfaces;
Expand Down Expand Up @@ -58,21 +57,21 @@ public Task<bool> Delete(string userId)

public Task<User?> GetUserByEmail(string email, bool sanitize = true)
{
var user = _users.Find(u => u.Email.ToLowerInvariant() == email.ToLowerInvariant());
var user = _users.Find(u => u.Email.Equals(email, StringComparison.OrdinalIgnoreCase));
return Task.FromResult(user);
}

public Task<User?> GetUserByEmailOrUsername(string emailOrUsername, bool sanitize = true)
{
var user = _users.Find(u =>
u.Email.ToLowerInvariant() == emailOrUsername.ToLowerInvariant() ||
u.Username.ToLowerInvariant() == emailOrUsername.ToLowerInvariant());
u.Email.Equals(emailOrUsername, StringComparison.OrdinalIgnoreCase) ||
u.Username.Equals(emailOrUsername, StringComparison.OrdinalIgnoreCase));
return Task.FromResult(user);
}

public Task<User?> GetUserByUsername(string username, bool sanitize = true)
{
var user = _users.Find(u => u.Username.ToLowerInvariant() == username.ToLowerInvariant());
var user = _users.Find(u => u.Username.Equals(username, StringComparison.OrdinalIgnoreCase));
return Task.FromResult(user);
}

Expand Down Expand Up @@ -102,11 +101,8 @@ public Task<ResultOfUpdate> ChangePassword(string userId, string password)
}
}

[Serializable]
internal class UserCreationException : Exception
internal sealed class UserCreationException : Exception
{
public UserCreationException() { }

protected UserCreationException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}
10 changes: 5 additions & 5 deletions Backend/BackendFramework.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AnalysisMode>Recommended</AnalysisMode>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CA1305;CA1816;CA1848;CS1591</NoWarn>
<NoWarn>$(NoWarn);CA1305;CA1848;CS1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RelaxNG" Version="3.2.3" >
<NoWarn>NU1701</NoWarn>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.28" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.35.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.35.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.3" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.5.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
<PackageReference Include="MailKit" Version="4.6.0" />
Expand Down
9 changes: 4 additions & 5 deletions Backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Docker multi-stage build
FROM mcr.microsoft.com/dotnet/sdk:6.0.423-focal-amd64 AS builder
FROM mcr.microsoft.com/dotnet/sdk:8.0.301-jammy-amd64 AS builder
WORKDIR /app

# Copy csproj and restore (fetch dependencies) as distinct layers.
Expand All @@ -11,7 +11,7 @@ COPY . ./
RUN dotnet publish -c Release -o build

# Build runtime image.
FROM mcr.microsoft.com/dotnet/aspnet:6.0.31-focal-amd64
FROM mcr.microsoft.com/dotnet/aspnet:8.0.6-jammy-amd64

ENV ASPNETCORE_URLS=http://+:5000
ENV COMBINE_IS_IN_CONTAINER=1
Expand All @@ -33,9 +33,8 @@ RUN apt-get update \
# Create the home directory for the new app user.
RUN mkdir -p $HOME

# Create an app user so the program doesn't run as root.
RUN groupadd -r app && \
useradd -r -g app -d $HOME -s /sbin/nologin -c "Docker image user" app
# Modify the existing app user to prevent login.
RUN usermod -s /sbin/nologin -c "Docker image user" app

## Set up application install directory.
RUN mkdir $APP_HOME && \
Expand Down
3 changes: 1 addition & 2 deletions Backend/Helper/DuplicateFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BackendFramework.Interfaces;
using BackendFramework.Models;

namespace BackendFramework.Helper
{
public class DuplicateFinder
{
private readonly IEditDistance _editDist;
private readonly LevenshteinDistance _editDist;
private readonly int _maxInList;
private readonly int _maxLists;
private readonly int _maxScore;
Expand Down
6 changes: 0 additions & 6 deletions Backend/Helper/FileOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace BackendFramework.Helper
{
[Serializable]
public class InvalidFileException : Exception
{
public InvalidFileException(string message) : base(message) { }

protected InvalidFileException(SerializationInfo info, StreamingContext context)
: base(info, context) { }

}

/// <summary>
Expand Down
7 changes: 1 addition & 6 deletions Backend/Helper/FileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;

namespace BackendFramework.Helper
{
Expand All @@ -26,13 +25,9 @@ public enum FileType
}

/// <summary> Indicates that an error occurred locating the current user's home directory. </summary>
[Serializable]
public class HomeFolderNotFoundException : Exception
public sealed class HomeFolderNotFoundException : Exception
{
public HomeFolderNotFoundException() { }

protected HomeFolderNotFoundException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
}

/// <summary>
Expand Down
6 changes: 0 additions & 6 deletions Backend/Helper/LiftHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using BackendFramework.Models;
using SIL.Lift.Parsing;

namespace BackendFramework.Helper
{
[Serializable]
public class InvalidLiftFileException : InvalidFileException
{
public InvalidLiftFileException(string message) : base("Malformed LIFT file: " + message) { }

protected InvalidLiftFileException(SerializationInfo info, StreamingContext context)
: base(info, context) { }

}

public static class TraitNames
Expand Down
11 changes: 2 additions & 9 deletions Backend/Helper/Sanitization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,20 @@
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace BackendFramework.Helper
{
/// <summary> Indicates an invalid input file name. </summary>
[Serializable]
public class InvalidFileNameException : Exception
public sealed class InvalidFileNameException : Exception
{
public InvalidFileNameException() : base() { }

protected InvalidFileNameException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}

/// <summary> Indicates an invalid input id. </summary>
[Serializable]
public class InvalidIdException : Exception
public sealed class InvalidIdException : Exception
{
public InvalidIdException() { }

protected InvalidIdException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}

public static class Sanitization
Expand Down
3 changes: 1 addition & 2 deletions Backend/Repositories/BannerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public async Task<ResultOfUpdate> Update(SiteBanner banner)
return ResultOfUpdate.Updated;
}

[Serializable]
private class BannerNotFound : Exception { }
private sealed class BannerNotFound : Exception { }
}
}
13 changes: 7 additions & 6 deletions Backend/Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public async Task<bool> Delete(string userId)
public async Task<User?> GetUserByEmail(string email, bool sanitize = true)
{
var user = (await _userDatabase.Users.FindAsync(
x => x.Email.ToLowerInvariant() == email.ToLowerInvariant())).FirstOrDefault();
x => x.Email.Equals(email, StringComparison.OrdinalIgnoreCase))).FirstOrDefault();
if (sanitize && user is not null)
{
user.Sanitize();
Expand All @@ -131,8 +131,9 @@ public async Task<bool> Delete(string userId)
public async Task<User?> GetUserByEmailOrUsername(string emailOrUsername, bool sanitize = true)
{
var lower = emailOrUsername.ToLowerInvariant();
var user = (await _userDatabase.Users.FindAsync(
u => u.Username.ToLowerInvariant() == lower || u.Email.ToLowerInvariant() == lower)).FirstOrDefault();
var user = (await _userDatabase.Users.FindAsync(u =>
u.Username.Equals(emailOrUsername, StringComparison.OrdinalIgnoreCase) ||
u.Email.Equals(emailOrUsername, StringComparison.OrdinalIgnoreCase))).FirstOrDefault();
if (sanitize && user is not null)
{
user.Sanitize();
Expand All @@ -145,7 +146,7 @@ public async Task<bool> Delete(string userId)
public async Task<User?> GetUserByUsername(string username, bool sanitize = true)
{
var user = (await _userDatabase.Users.FindAsync(
x => x.Username.ToLowerInvariant() == username.ToLowerInvariant())).FirstOrDefault();
x => x.Username.Equals(username, StringComparison.OrdinalIgnoreCase))).FirstOrDefault();
if (sanitize && user is not null)
{
user.Sanitize();
Expand All @@ -171,12 +172,12 @@ public async Task<ResultOfUpdate> Update(string userId, User user, bool updateIs
}

// Confirm that email and username aren't taken by another user.
if (user.Email.ToLowerInvariant() != oldUser.Email.ToLowerInvariant()
if (!user.Email.Equals(oldUser.Email, StringComparison.OrdinalIgnoreCase)
&& await GetUserByEmail(user.Email) is not null)
{
return ResultOfUpdate.Failed;
}
if (user.Username.ToLowerInvariant() != oldUser.Username.ToLowerInvariant()
if (!user.Username.Equals(oldUser.Username, StringComparison.OrdinalIgnoreCase)
&& await GetUserByUsername(user.Username) is not null)
{
return ResultOfUpdate.Failed;
Expand Down
Loading

0 comments on commit b4d3809

Please sign in to comment.