Skip to content

New server function getServerEnvironmentValue #1937

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

Conversation

pentaflops
Copy link
Contributor

A useful function for storing passwords and other settings if using docker images

str getServerEnvironmentValue( str envName )

@AlexTMjugador
Copy link
Member

AlexTMjugador commented Dec 22, 2020

I like the idea of this PR. Environment variables are a nice way to pass parameters to processes in an easy to automate way, and without requiring command line parameters. I don't know why someone downvoted this, as you can pass a hash of sensitive data, like you would do in a database, and already use modules to retrieve this information.

Anyway, I'd use the new argument parser for this function. I'd also make sure that it is restricted in the ACL by default, so that no random resource can get environment variables unless the admin explicitly allows it, for privacy concerns. Finally, I'd rename the function to getServerEnvironmentVariable, but this is up to discussion.

@Pirulax
Copy link
Contributor

Pirulax commented Dec 24, 2020

I'd rename it to getServerEnvironmentVariable (in case we decide to add some kind of support to the client in the future)

@@ -111,6 +111,7 @@ class CLuaFunctionDefs
LUA_DECLARE(SetServerPassword);
LUA_DECLARE(GetServerConfigSetting);
LUA_DECLARE(SetServerConfigSetting);
LUA_DECLARE(GetServerEnvironmentValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static std::string_view CLuaFunctionDefs::GetServerEnvironmentValue(std::string name) ;

@@ -233,6 +233,7 @@ void CLuaManager::LoadCFunctions()
{"getServerPassword", CLuaFunctionDefs::GetServerPassword},
{"setServerPassword", CLuaFunctionDefs::SetServerPassword},
{"getServerConfigSetting", CLuaFunctionDefs::GetServerConfigSetting},
{"getServerEnvironmentValue", CLuaFunctionDefs::GetServerEnvironmentValue},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 {"getServerEnvironmentValue", ArgumentParser<GetServerEnvironmentValue>}

Also, for this to work, you need to move it to CLuaUtilDefs.
The new parser doesn't work in CLuaManager.

Comment on lines +689 to +707
int CLuaFunctionDefs::GetServerEnvironmentValue(lua_State* luaVM)
{
SString strName;

CScriptArgReader argStream(luaVM);
argStream.ReadString(strName);

if (!argStream.HasErrors())
{
lua_pushstring(luaVM, getenv(strName));
return 1;
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());

lua_pushboolean(luaVM, false);
return 1;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string_view CLuaFunctionDefs::GetServerEnvironmentValue(std::string name) 
{
    return getenv(name.c_str());
|

One thing im not sure about: What if getenv returns null? Does Lua handle it correctly?

@Pirulax
Copy link
Contributor

Pirulax commented Dec 24, 2020

Also, there is a native Lua function called os.getenv. We could enable it on the server, instead of adding a non-native function. It calls the same function we do, since Lua is pure ANSI C.

@prnxdev
Copy link

prnxdev commented Dec 26, 2020

What is difference between this nad get and set functions? I once wrote a script that was connecting to MySQL database and I had different auth credentials for local and production so I used those functions to manage those data. Problem is that get function read data correctly but It adds * to every key (as I remember).

In other way there is also fileWrite and fileOpen functions. I don't know how exactly this function works but if it reads data from .env file I think it's not so important to have one in MTA API.

@patrikjuvonen patrikjuvonen added the enhancement New feature or request label Dec 29, 2020
@patrikjuvonen
Copy link
Contributor

What are the security concerns regarding getenv and the scope of variables one could have access to?

@Disinterpreter
Copy link
Member

What are the security concerns regarding getenv and the scope of variables one could have access to?

It is only server side feature and I don't see the problems with accessing to the scope in this feature. If we think about unnecessary security of serverside we risky to lost all connections with OS as it was in: #1782

I can find thousand programs which work with ENV and it's okay.

@patrikjuvonen
Copy link
Contributor

I believe we could try implementing the ACL system from the Lua modules branch and then allow getenv through the native Lua os function as suggested by @Pirulax

@AlexTMjugador
Copy link
Member

AlexTMjugador commented Dec 30, 2020

What is difference between this nad get and set functions? I once wrote a script that was connecting to MySQL database and I had different auth credentials for local and production so I used those functions to manage those data. Problem is that get function read data correctly but It adds * to every key (as I remember).

In other way there is also fileWrite and fileOpen functions. I don't know how exactly this function works but if it reads data from .env file I think it's not so important to have one in MTA API.

Those functions solve a different problem: allowing the server administrator to set up configuration values for the resources. Environment variables are not meant to store resource configuration; instead, they represent more generic data about the OS environment that may be useful to processes as a whole.

Of course storing the environment variables in files via external programs and reading them back in the MTA: SA Lua VM is a workaround, but that implies writes to a physical block device, which contributes to reducing the lifespan of SSDs (unless they are done in a RAM filesystem, but using such filesystems in Windows is awkward). And, honestly, this function is simple enough to be merged quickly and successfully, so it's not like that workaround is even needed.

Also, there is a native Lua function called os.getenv. We could enable it on the server, instead of adding a non-native function. It calls the same function we do, since Lua is pure ANSI C.

Good catch! I was concerned about how we can ACL native Lua functions, though, but @patrikjuvonen proposed a solution for that, so it's perfect.

It is only server side feature and I don't see the problems with accessing to the scope in this feature. If we think about unnecessary security of serverside we risky to lost all connections with OS as it was in: #1782

I can find thousand programs which work with ENV and it's okay.

As @sbx320 said in the discussion you linked to, backdoored and compiled server side resources are a concern. Allowing these functions by default might expose the server to be fingerprinted in undesirable ways that may not be easily noticed by the server owner, and, if the server owner improperly uses environment variables to store secrets like deployment keys, it may even hand down a potential attacker with enough information to compromise the system. Denying this function by default is a way of making the server owner aware of this, and if some random compiled DirectX GUI resource insists in needing access to functions like os.getenv, then it's easy to conclude that something is wrong with it.

@Disinterpreter
Copy link
Member

Disinterpreter commented Dec 30, 2020

Denying this function by default is a way of making the server owner aware of this, and if some random compiled DirectX GUI resource insists in needing access to functions like os.getenv, then it's easy to conclude that something is wrong with it.

I have no complaints of some alerts or ACL lists. It is good, really. But I hate when superfluous security on the server restrict me. I think os.getenv must be in MTA server. If you want to mark this function as dangerous and implement it with alerts of security - let it be so, this feature is useful.

@LosFaul
Copy link
Contributor

LosFaul commented Dec 30, 2020

in general I think unsafe Lua functions should be enabled/disabeled via meta.xml for server side
(default is always disabled)
to have it per resource rather than for whole server

@botder
Copy link
Member

botder commented Dec 30, 2020

Enable os.getenv but provide a custom C function to check if the executing resource has the ACL permission to do so and default to false if not given.

@Gallardo994
Copy link

Those functions solve a different problem: allowing the server administrator to set up configuration values for the resources. Environment variables are not meant to store resource configuration; instead, they represent more generic data about the OS environment that may be useful to processes as a whole.

Well, that's unless you're running an immutable Docker image and want to use different configurations for each instance, without any need to pass volumes or store any local data (totally stateless MTA server instance). This is totally normal for containers to have ENVs as configurations.

But yeah ACL would be required for other usecases, e.g. if you use third-party resources which you may not have sources of, or if ENVs have some sensitive data required only for a selected resource (read: passwords)

@patrikjuvonen
Copy link
Contributor

@pentaflops Are you willing to work on the os.getenv + ACL implementation or should this PR be closed and a new issue be created?

@patrikjuvonen patrikjuvonen marked this pull request as draft January 2, 2021 15:11
@patrikjuvonen patrikjuvonen added the feedback Further information is requested label Jan 2, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2022

This draft pull request is stale because it has been open for at least 90 days with no activity. Please continue on your draft pull request or it will be closed in 30 days automatically.

@github-actions github-actions bot added the stale Inactive for over 90 days, to be closed label Jan 7, 2022
@github-actions github-actions bot closed this Feb 7, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2022

This draft pull request was closed because it has been marked stale for 30 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feedback Further information is requested stale Inactive for over 90 days, to be closed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants