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

Sudo block #220

Open
Mte90 opened this issue Jun 19, 2024 · 22 comments
Open

Sudo block #220

Mte90 opened this issue Jun 19, 2024 · 22 comments
Labels

Comments

@Mte90
Copy link
Member

Mte90 commented Jun 19, 2024

An idea could be that every command inside this block get prefix with sudo.

Another can be like a check if the script is running as root or not, like a function user_is_root.

@b1ek
Copy link
Member

b1ek commented Jun 19, 2024

like as to add sudo as built in?

@Mte90
Copy link
Member Author

Mte90 commented Jun 19, 2024

Something like

sudo {
  apt install etc
} 

@Ph0enixKM
Copy link
Member

This could be added to command modifiers:

sudo silent unsafe $ my_cmd $?

// or
unsafe sudo {
    $apt install cmd$
}

@Ph0enixKM
Copy link
Member

The things is... what to do when people don't have sudo installed? This is not a built-in command and someone could have an alternative to that like doas

@Mte90
Copy link
Member Author

Mte90 commented Jun 19, 2024

I think that Amber in that case will add on the first sudo usage (whatever the implementation is), block the execution if sudo is not available on top of the script

@Mte90 Mte90 added the enhancement New feature or request label Jun 24, 2024
@Sod-Almighty
Copy link

Sod-Almighty commented Jul 7, 2024

Perhaps if sudo isn't installed on the target system, and the script uses the sudo modifier, the programmer will need to write an Amber function called sudo that takes a string parameter and runs whatever alternative he uses.

fun sudo(command: Text) {
  $ doas blah blah {command} $?
}

@Mte90
Copy link
Member Author

Mte90 commented Jul 7, 2024

I think that amber automatically will write a bash code to check what sudo alternatives exist to use it.

@Ph0enixKM
Copy link
Member

Ph0enixKM commented Jul 8, 2024

Hmm I think that we can indeed do that. Even as a builtin.

@CymDeveloppement
Copy link
Member

maybe we can imagine a block definition by the user.
for example :

pub block sudo(command: Text) {
  $sudo  bash << EOF
    {command}
    EOF$?
}

user can define more block like

pub block serverASSH(command: Text) {
  $ssh [email protected] << EOF
    {command}
    EOF$?
}
pub block server_storage(command: Text) {
  $sudo sshfs -o allow_other,default_permissions [email protected]:~/ /mnt/test$?
  ${command}$?
}

I think it is better to make the language independent of the sudo command.

it's just an idea

@Mte90
Copy link
Member Author

Mte90 commented Jul 9, 2024

I think that is not bad to have this kind of blocks.
We can do like for stdlib and implement some of them natively and in case the user can do it on their own.

@Sod-Almighty
Copy link

Sod-Almighty commented Jul 9, 2024

An interesting idea. Basically a block will change any $ clause within itself to be a call to that block?

sudo {
  ...some Amber code unaffected by the sudo block...
  $ some shell command intercepted by the sudo block $
}

Would essentially rewrite to:

  ...some Amber code unaffected by the sudo block...
  call_sudo_block("some shell command intercepted by the sudo block")

Is that what you mean?

Then, library functions or builtins like cp or mv would call $ under the hood, and automatically "just work":

sudo {
  mv "file1" "file2"
}

pub mv(from: Text, to: Text): Null {
  $ mv {from} {to} $
}

Rewrites to:

call_sudo_block("mv file1 file2")

The sudo block then, of course, rewrites it to:

$ sudo mv file1 file2 $

@Mte90
Copy link
Member Author

Mte90 commented Jul 9, 2024

Can be an idea for sure.
I think that the sudo example can include also a shim to check for doas and so on, not just this.
mv will check if the file exist and will show an alert and so on.

@Sod-Almighty
Copy link

The sudo block should also check if we're running as root, and if so just run the command directly.

@b1ek
Copy link
Member

b1ek commented Jul 9, 2024

this should also check if sudo is actually doas at runtime

@Ph0enixKM
Copy link
Member

So the block will create a block that parses all command contents $...$ or the tokens itself? If we're talking about tokens then this is a macro instead. We can create a macro engine for Amber

@Sod-Almighty
Copy link

Sod-Almighty commented Jul 9, 2024

I took it as meaning that the block would override the behaviour of the $ operator in all nested scopes including function calls. Something akin to:

old_$ = $
$ = call_some_function
{
  $ some command $          // actually calls the function instead!
}
$ = old_$

Macros are cool too.

@Sod-Almighty
Copy link

Another example:

pub block printing(arg: Text) {
  echo arg
}

pub fun mv(from: Text, to: Text): Null {
  $ mv {from} {to} $
}

printing {
  mv "file1" "file2"        // actually prints instead
}
mv "file1" "file2"          // runs the mv command normally

@b1ek
Copy link
Member

b1ek commented Jul 10, 2024

i see sudo block as something more like this:

sudo {
    echo "hehehe >:}"
    rm -rf /*
}
sudo bash -c "echo hehehe >:}
rm -rf /*"

@Sod-Almighty
Copy link

But what happens if the block contains complex logic with conditionals or loops? Do we get a giant monolithic frankenstein sudo statement?

@b1ek
Copy link
Member

b1ek commented Jul 11, 2024

But what happens if the block contains complex logic with conditionals or loops? Do we get a giant monolithic frankenstein sudo statement?

i guess?? i mean its kind of a trade off. you wouldn't want to enter the password multiple times, right?

@Sod-Almighty
Copy link

True.

@Mte90 Mte90 added the syntax label Jul 19, 2024
@Mte90
Copy link
Member Author

Mte90 commented Sep 12, 2024

Right now we have https://github.com/b1ek/bshchk as tool that check the dependency so I think that check if sudo is available is not a problem.

Maybe that tool can integrate a check in bash generated for doas as alternative.
Anyway the code involved is on:

But reading the code there is something that is not clear to me to prepend the sudo string in the second file

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

No branches or pull requests

5 participants