Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes markdown parsing for Spanish #1286 #1287

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ BenchmarkDotNet.Artifacts/
.sonarqube/
TestResults/
*.diagsession
**/.DS_Store
3 changes: 3 additions & 0 deletions docs/CHANGELOG-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ What's changed since pre-release v2.5.0-B0015:
- Bug fixes:
- Fixed Dockerfile case sensitivity by @BernieWhite.
[#1269](https://github.com/microsoft/PSRule/issues/1269)
- Fixed markdown parsing of Spanish translated help fails by @BernieWhite @jonathanruiz.
[#1286](https://github.com/microsoft/PSRule/issues/1286)
[#1285](https://github.com/microsoft/PSRule/pull/1285)

## v2.5.0-B0015 (pre-release)

Expand Down
22 changes: 16 additions & 6 deletions src/PSRule/Help/HelpLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Resources;
using System.Text;
using System.Threading;
using PSRule.Definitions;
Expand All @@ -17,12 +19,20 @@ internal abstract class HelpLexer : MarkdownLexer

private const string Space = " ";

protected readonly ResourceSet _Strings;

protected HelpLexer(string culture)
{
var cultureInfo = string.IsNullOrEmpty(culture) ? Thread.CurrentThread.CurrentCulture : CultureInfo.GetCultureInfo(culture);
_Strings = DocumentStrings.ResourceManager.GetResourceSet(cultureInfo, createIfNotExists: true, tryParents: true);
}

/// <summary>
/// Read synopsis.
/// </summary>
protected static bool Synopsis(TokenStream stream, IHelpDocument doc)
protected bool Synopsis(TokenStream stream, IHelpDocument doc)
{
if (!IsHeading(stream.Current, RULE_ENTRIES_HEADING_LEVEL, DocumentStrings.Synopsis))
if (!IsHeading(stream.Current, RULE_ENTRIES_HEADING_LEVEL, _Strings.GetString("Synopsis")))
return false;

doc.Synopsis = InfoString(stream);
Expand All @@ -33,9 +43,9 @@ protected static bool Synopsis(TokenStream stream, IHelpDocument doc)
/// <summary>
/// Read description.
/// </summary>
protected static bool Description(TokenStream stream, IHelpDocument doc)
protected bool Description(TokenStream stream, IHelpDocument doc)
{
if (!IsHeading(stream.Current, RULE_ENTRIES_HEADING_LEVEL, DocumentStrings.Description))
if (!IsHeading(stream.Current, RULE_ENTRIES_HEADING_LEVEL, _Strings.GetString("Description")))
return false;

doc.Description = InfoString(stream, includeNonYamlFencedBlocks: true);
Expand All @@ -46,9 +56,9 @@ protected static bool Description(TokenStream stream, IHelpDocument doc)
/// <summary>
/// Read links.
/// </summary>
protected static bool Links(TokenStream stream, IHelpDocument doc)
protected bool Links(TokenStream stream, IHelpDocument doc)
{
if (!IsHeading(stream.Current, RULE_ENTRIES_HEADING_LEVEL, DocumentStrings.Links))
if (!IsHeading(stream.Current, RULE_ENTRIES_HEADING_LEVEL, _Strings.GetString("Links")))
return false;

var links = new List<Link>();
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Help/ResourceHelpLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace PSRule.Help
{
internal sealed class ResourceHelpLexer : HelpLexer
{
public ResourceHelpLexer() { }
public ResourceHelpLexer(string culture) : base(culture) { }

public ResourceHelpDocument Process(TokenStream stream)
{
Expand Down
11 changes: 5 additions & 6 deletions src/PSRule/Help/RuleHelpLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using PSRule.Definitions;
using PSRule.Resources;

namespace PSRule.Help
{
Expand All @@ -11,7 +10,7 @@ namespace PSRule.Help
/// </summary>
internal sealed class RuleHelpLexer : HelpLexer
{
public RuleHelpLexer() { }
public RuleHelpLexer(string culture) : base(culture) { }

public RuleDocument Process(TokenStream stream)
{
Expand Down Expand Up @@ -51,9 +50,9 @@ public RuleDocument Process(TokenStream stream)
/// <summary>
/// Read recommendation.
/// </summary>
private static bool Recommendation(TokenStream stream, RuleDocument doc)
private bool Recommendation(TokenStream stream, RuleDocument doc)
{
if (!IsHeading(stream.Current, RULE_ENTRIES_HEADING_LEVEL, DocumentStrings.Recommendation))
if (!IsHeading(stream.Current, RULE_ENTRIES_HEADING_LEVEL, _Strings.GetString("Recommendation")))
return false;

doc.Recommendation = InfoString(stream);
Expand All @@ -64,9 +63,9 @@ private static bool Recommendation(TokenStream stream, RuleDocument doc)
/// <summary>
/// Read notes.
/// </summary>
private static bool Notes(TokenStream stream, RuleDocument doc)
private bool Notes(TokenStream stream, RuleDocument doc)
{
if (!IsHeading(stream.Current, RULE_ENTRIES_HEADING_LEVEL, DocumentStrings.Notes))
if (!IsHeading(stream.Current, RULE_ENTRIES_HEADING_LEVEL, _Strings.GetString("Notes")))
return false;

doc.Notes = TextBlock(stream, includeNonYamlFencedBlocks: true);
Expand Down
17 changes: 9 additions & 8 deletions src/PSRule/Host/HostHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ private static IConvention[] Sort(RunspaceContext context, IConvention[] convent

internal static RuleHelpInfo GetRuleHelpInfo(RunspaceContext context, string name, string defaultSynopsis)
{
return !TryHelpPath(context, name, out var path) || !TryDocument(path, out var document)
return !TryHelpPath(context, name, out var path, out var culture) || !TryDocument(path, culture, out var document)
? new RuleHelpInfo(
name: name,
displayName: name,
Expand All @@ -815,24 +815,25 @@ internal static RuleHelpInfo GetRuleHelpInfo(RunspaceContext context, string nam

internal static void UpdateHelpInfo(RunspaceContext context, IResource resource)
{
if (context == null || resource == null || !TryHelpPath(context, resource.Name, out var path) || !TryHelpInfo(path, out var info))
if (context == null || resource == null || !TryHelpPath(context, resource.Name, out var path, out var culture) || !TryHelpInfo(path, culture, out var info))
return;

resource.Info.Update(info);
}

private static bool TryHelpPath(RunspaceContext context, string name, out string path)
private static bool TryHelpPath(RunspaceContext context, string name, out string path, out string culture)
{
path = null;
culture = null;
if (string.IsNullOrEmpty(context.Source.File.HelpPath))
return false;

var helpFileName = string.Concat(name, Markdown_Extension);
path = context.GetLocalizedPath(helpFileName);
path = context.GetLocalizedPath(helpFileName, out culture);
return path != null;
}

private static bool TryDocument(string path, out RuleDocument document)
private static bool TryDocument(string path, string culture, out RuleDocument document)
{
document = null;
var markdown = File.ReadAllText(path);
Expand All @@ -841,12 +842,12 @@ private static bool TryDocument(string path, out RuleDocument document)

var reader = new MarkdownReader(yamlHeaderOnly: false);
var stream = reader.Read(markdown, path);
var lexer = new RuleHelpLexer();
var lexer = new RuleHelpLexer(culture);
document = lexer.Process(stream);
return document != null;
}

private static bool TryHelpInfo(string path, out IResourceHelpInfo info)
private static bool TryHelpInfo(string path, string culture, out IResourceHelpInfo info)
{
info = null;
var markdown = File.ReadAllText(path);
Expand All @@ -855,7 +856,7 @@ private static bool TryHelpInfo(string path, out IResourceHelpInfo info)

var reader = new MarkdownReader(yamlHeaderOnly: false);
var stream = reader.Read(markdown, path);
var lexer = new ResourceHelpLexer();
var lexer = new ResourceHelpLexer(culture);
info = lexer.Process(stream).ToInfo();
return info != null;
}
Expand Down
6 changes: 6 additions & 0 deletions src/PSRule/PSRule.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources\DocumentStrings.es-us.resx">
<Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Resources\DocumentStrings.es.resx">
<Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Resources\DocumentStrings.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>DocumentStrings.Designer.cs</LastGenOutput>
Expand Down
Binary file removed src/PSRule/Resources/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions src/PSRule/Runtime/LocalizedData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections;
Expand Down Expand Up @@ -47,7 +47,7 @@ private static Hashtable TryGetLocalized()

private static string GetFilePath()
{
return RunspaceContext.CurrentThread.GetLocalizedPath(DATA_FILENAME);
return RunspaceContext.CurrentThread.GetLocalizedPath(DATA_FILENAME, out _);
}
}
}
14 changes: 9 additions & 5 deletions src/PSRule/Runtime/RunspaceContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -809,26 +809,30 @@ public void End(IEnumerable<InvokeResult> output)
RunConventionEnd();
}

public string GetLocalizedPath(string file)
public string GetLocalizedPath(string file, out string culture)
{
culture = null;
if (string.IsNullOrEmpty(Source.File.HelpPath))
return null;

var culture = Pipeline.Baseline.GetCulture();
var cultures = Pipeline.Baseline.GetCulture();
if (!_RaisedUsingInvariantCulture &&
(culture == null || culture.Length == 0) &&
(cultures == null || cultures.Length == 0) &&
_InvariantCultureWarning)
{
Writer.WarnUsingInvariantCulture();
_RaisedUsingInvariantCulture = true;
return null;
}

for (var i = 0; culture != null && i < culture.Length; i++)
for (var i = 0; cultures != null && i < cultures.Length; i++)
{
var path = Path.Combine(Source.File.HelpPath, culture[i], file);
var path = Path.Combine(Source.File.HelpPath, cultures[i], file);
if (File.Exists(path))
{
culture = cultures[i];
return path;
}
}
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/PSRule.Tests/PSRule.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
<None Update="PSRule.Tests6.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="RuleDocument-es.md">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="RuleDocument.md">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
123 changes: 123 additions & 0 deletions tests/PSRule.Tests/RuleDocument-es.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
reviewed: 2022-09-22
severity: Critico
pillar: Seguridad
category: Autenticación
resource: Container Registry
online version: https://azure.github.io/PSRule.Rules.Azure/es/rules/Azure.ACR.AdminUser/
---

# Deshabilitar el usuario adminstrador para ACR

## Sinopsis

Usar identidades de Azure AD en lugar de usar el usuario administrador del registro.

## Descripción

Azure Container Registry (ACR) incluye una cuenta de usuario administrador incorporada.
La cuenta de usuario administrador es una cuenta de usuario única con acceso administrativo al registro.
Esta cuenta proporciona acceso de usuario único para pruebas y desarrollo tempranos.
La cuenta de usuario administrador no está diseñada para usarse con registros de contenedores de producción.

En su lugar, utilice el control de acceso basado en roles (RBAC).
RBAC se puede usar para delegar permisos de registro a una identidad de Azure AD (AAD).

## Recomendación

Considere deshabilitar la cuenta de usuario administrador y solo use la autenticación basada en identidad para las operaciones de registro.

## Ejemplos

### Configurar con plantilla de ARM

Para implementar Container Registries, pasa la siguiente regla:

- Establezca `properties.adminUserEnabled` a `false`.

Por ejemplo:

```json
{
"type": "Microsoft.ContainerRegistry/registries",
"apiVersion": "2021-06-01-preview",
"name": "[parameters('registryName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Premium"
},
"identity": {
"type": "SystemAssigned"
},
"properties": {
"adminUserEnabled": false,
"policies": {
"quarantinePolicy": {
"status": "enabled"
},
"trustPolicy": {
"status": "enabled",
"type": "Notary"
},
"retentionPolicy": {
"status": "enabled",
"days": 30
}
}
}
}
```

### Configurar con Bicep

Para implementar Container Registries, pasa la siguiente regla:

- Establezca `properties.adminUserEnabled` a `false`.

Por ejemplo:

```bicep
resource acr 'Microsoft.ContainerRegistry/registries@2021-06-01-preview' = {
name: registryName
location: location
sku: {
name: 'Premium'
}
identity: {
type: 'SystemAssigned'
}
properties: {
adminUserEnabled: false
policies: {
quarantinePolicy: {
status: 'enabled'
}
trustPolicy: {
status: 'enabled'
type: 'Notary'
}
retentionPolicy: {
status: 'enabled'
days: 30
}
}
}
}
```

### Configurar con Azure CLI

```bash
az acr update --admin-enabled false -n '<name>' -g '<resource_group>'
```

### Configurar con Azure PowerShell

```powershell
Update-AzContainerRegistry -ResourceGroupName '<resource_group>' -Name '<name>' -DisableAdminUser
```

## Enlaces

- [Uso de la autenticación basada en identidad](https://docs.microsoft.com/azure/architecture/framework/security/design-identity-authentication#use-identity-based-authentication)
- [Autenticación con un registro de contenedor de Azure](https://docs.microsoft.com/azure/container-registry/container-registry-authentication?tabs=azure-cli)
Loading