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

Json.lns of augeas leaves policy.json files in readable, but messy state #820

Open
MrStupnikov opened this issue Oct 27, 2023 · 1 comment

Comments

@MrStupnikov
Copy link

Originally this problem was reported by one of OpenStack operators : when he used TripleO to tune API policies for one of the services, he ended up having valid JSON file, which looked invalid from operator's perspective. After taking a second look at this issue we have figured out that this is cause by Json.lns. I want to kindly ask you to take a look at the following steps to reproduce and let us know if this is a cosmetic issue that could be fixed or a feature.

root@ubuntu:~# cat /tmp/policy.json
{
    "context_is_admin": "role:admin or role:load-balancer_admin",
    "admin_or_owner": "is_admin:True or project_id:%(project_id)s",

    "load-balancer:read": "rule:admin_or_owner",
    "load-balancer:read-global": "is_admin:True",
    "load-balancer:write": "rule:admin_or_owner",
    "load-balancer:read-quota": "rule:admin_or_owner",
    "load-balancer:read-quota-global": "is_admin:True",
    "load-balancer:write-quota": "is_admin:True"
}
root@ubuntu:~# augtool --version
augtool 1.14.1 <http://augeas.net/>
Copyright (C) 2007-2016 David Lutterkort
License LGPLv2+: GNU LGPL version 2.1 or later
                 <http://www.gnu.org/licenses/lgpl-2.1.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David Lutterkort
root@ubuntu:~# augtool
augtool> set /augeas/load/Json/lens Json.lns
augtool> set /augeas/load/Json/incl /tmp/policy.json
augtool> load
augtool> set /files/tmp/policy.json/dict/entry[last()+1] 'groups'
augtool> touch /files/tmp/policy.json/dict/entry[last()]/array
augtool> save
Saved 1 file(s)
augtool> quit
root@ubuntu:~# cat /tmp/policy.json
{
    "context_is_admin": "role:admin or role:load-balancer_admin",
    "admin_or_owner": "is_admin:True or project_id:%(project_id)s",

    "load-balancer:read": "rule:admin_or_owner",
    "load-balancer:read-global": "is_admin:True",
    "load-balancer:write": "rule:admin_or_owner",
    "load-balancer:read-quota": "rule:admin_or_owner",
    "load-balancer:read-quota-global": "is_admin:True",
    "load-balancer:write-quota": "is_admin:True"
,
"groups":[]}
@georgehansper
Copy link
Member

In general, Augeas lenses create files which are syntactically correct for a given syntax (such as Json), they do not necessarily make them "pretty".

Part of the problem is that for many languages such as JSON, spaces are not significant. So when a new entry is created, the amount of space that can or should be inserted becomes arbitrary.

In this case, it may be possible to update the lens to produce a more readable output, though I expect the indentation would remain messy.

I find it odd that appending another entry after "groups" inserts the new entry directly between previous one and the }

> augtool --noload  --noautoload 
augtool> set /augeas/load/Json/lens Json.lns
augtool> set /augeas/load/Json/incl /tmp/policy.json
augtool> load-file /tmp/policy.json
augtool> set /files/tmp/policy.json/dict/entry[last()+1] 'extra1'
augtool> set /files/tmp/policy.json/dict/entry[.='extra1']/string 'hello'
augtool> set /files/tmp/policy.json/dict/entry[last()+1] 'extra2'
augtool> set /files/tmp/policy.json/dict/entry[.='extra2']/string 'world'
augtool> preview /files/tmp/policy.json/
{
    "context_is_admin": "role:admin or role:load-balancer_admin",
    "admin_or_owner": "is_admin:True or project_id:%(project_id)s",

    "load-balancer:read": "rule:admin_or_owner",
    "load-balancer:read-global": "is_admin:True",
    "load-balancer:write": "rule:admin_or_owner",
    "load-balancer:read-quota": "rule:admin_or_owner",
    "load-balancer:read-quota-global": "is_admin:True",
    "load-balancer:write-quota": "is_admin:True"
,
"extra1":"hello","extra2":"world"}augtool> 

There is definitely room for improvement here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants